Arduino | Servo Knob

Overview

Move a servo using the knob

The 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

  1. Connect the servo to the D8 connection on the Animate shield.
  2. Connect the board to your computer using the USB-C cable.
  3. Upload the code (for Arduino IDE uploading see Upload a Sketch in Arduino IDE)
  4. Power the board using the DC jack on the animate shield to provide servo power.

Code Breakdown

  1. Setup()
    1. Attached the servo on pin 8
  2. Loop()
    1. Read the input value from the A0 pin (knob)
    2. Map the value from 0-1023 to 0-180
    3. 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)