Arduino | Servo Knob
Overview
Move a servo using the knobThe Code
LAUNCH BLOCK CODE#include <Servo.h> Servo myservo8; void setup() { myservo8.attach(8); } void loop() { // Read the value from analog pin 5 and map to servo value myservo8.write((map(analogRead(A0),0,1024,0,180))); }
Code Breakdown
Set-up
- Connect the servo to the D8 connection on the Animate shield.
- Connect the board to your computer using the USB-C cable.
- Upload the code (for Arduino IDE uploading see Upload a Sketch in Arduino IDE)
- Power the board using the DC jack on the animate shield to provide servo power.
Code Breakdown
- Setup()
- Attached the servo on pin 8
- Loop()
- Read the input value from the A0 pin (knob)
- Map the value from 0-1023 to 0-180
- Write the mapped value to the servo
Blocks Used
- In/Out > Analog > Analog Read
- Pin = A0
- Math > Map (second one with 4 inputs)
- Value = Analog Read
- fromLow = 0
- fromHigh = 1023
- toLow = 0
- toHigh = 180
- *Servo -> Write Pin | Angle
- Write Pin = 8
- Value = Map
*Note – if the servo blocks are unavailable in the sidebar, you can enable them under the options (gear menu)