UCLA BMES Build Team

Workshop 3 - ESP32 Analog

Activity 2 - Servo Motors

Task:

Circuit:

Circuit

Code to Automatically Sweep Servo:

#include<Servo.h>
Servo Right;
bool current = false;
int rotation = 0;
void setup() {
  Right.attach(A5);
  Serial.begin(9600);
}

void loop()
{
   for (int i = 0; i <= 180 ; i++)
   {
      if (rotation%2==0)
        {Right.write(i);}
      else
        {Right.write(180-i);}
      delay(10);
   }
   rotation++;
}

Code to Manually Control Servo with Joystick:

#include<Servo.h>
Servo Right;
int joyY = A0; // analog pin used to connect the Y - axis of Joystick
int y; // variables to read the values from the analog pins

void setup() {
  Right.attach(A5);
  Serial.begin(9600);
}

void loop() {
  y = joyY;    // reads the value of the Joystick's Y - axis (value between 0 and 1023)
  y = map(analogRead(joyY),0,1023, 0, 180);
  Right.write(y);
  delay(15);
}