Skip to content

Output devices

Group assignment:

  • Measure the power consumption of an output device.

Objective

The objective of this group assignment is to measure the actual power consumption of an output device: here, a servo motor controlled by an ESP32 board. We used a stabilized lab power supply to directly measure the voltage, current, and power consumed by the setup.


Materials used

Item Description
Microcontroller board ESP32
Actuator (output device) SG90 servo motor
Power supply Stabilized lab power supply (voltage, current, and power displayed)
Wiring Jumper wires, breadboard
Software Arduino IDE

🔌 Wiring diagram

  • Signal (PWM) of the servo motor → GPIO 23
  • VCC of the servo motor → ESP32 VCC
  • GND → ESP32 GND

💻 Code used

We used the example provided by the Servo library in the Arduino IDE (File > Examples > ESP32Servo > Sweep), which sweeps the servo motor from 0° to 180° continuously.

#include <ESP32Servo.h>
Servo myservo;  // create servo object to control a servo
// 16 servo objects can be created on the ESP32
int pos = 0;    // variable to store the servo position
// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33 
// Possible PWM GPIO pins on the ESP32-S2: 0(used by on-board button),1-17,18(used by on-board LED),19-21,26,33-42
#if defined(ARDUINO_ESP32S2_DEV)
int servoPin = 23;
#else
int servoPin = 23;
#endif
void setup() {
    // Allow allocation of all timers
    ESP32PWM::allocateTimer(0);
    ESP32PWM::allocateTimer(1);
    ESP32PWM::allocateTimer(2);
    ESP32PWM::allocateTimer(3);
    myservo.setPeriodHertz(50); 
    myservo.attach(servoPin, 1000, 2000); 
}
void loop() {
    for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
        // in steps of 1 degree
        myservo.write(pos);    // tell servo to go to position in variable 'pos'
        delay(15);             // waits 15ms for the servo to reach the position
    }
    for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
        myservo.write(pos);    // tell servo to go to position in variable 'pos'
        delay(15);             // waits 15ms for the servo to reach the position
    }
}

Note: on the ESP32, it is recommended to use the ESP32Servo library (instead of the standard Arduino Servo.h library, which is not natively compatible with the ESP32).


Video