9. Output devices




Group assignment



This week was working with output devices for our boards that we made in week 8. The idea is to test them and get them working. We were given a choice of different options to use, from LCD displays, OLED displays, motors, speakers, etc. The component or actuator I chose is a servo motor, specifically a micro servo. You can check out the group assignment at the following link.


Micro servo SG90




The SG90 micro servo is a small, lightweight actuator popular in robotics due to its low cost and ease of use. Capable of rotating up to 180 degrees, it allows precise control over motion, making it ideal for applications such as robotic arms, steerable vehicles, and model airplane control systems. This servo typically operates between 4.8 and 6.0 volts, and its compact size, coupled with its wide range of motion, offers a versatile and efficient solution for mechanically controlling a wide range of components in electronic projects.

Its operation is based on the ability to rotate its shaft to specific positions, which are determined by angles. Upon receiving an electrical signal, the servo's internal circuitry interprets this information and adjusts the motor position to that corresponding to the desired angle. This position control is accomplished by pulse width modulation (PWM) pulses, where the duration of the pulse sent to the servo determines the angle at which it should rotate. For example, a pulse of a certain duration might cause the servo to turn 90 degrees, while another pulse of a different duration might take it to 0 or 180 degrees.

Features



  • Torque: 1.6kg - cm (4.8V)
  • Operating temperature: -30 to +60 degrees Celsius
  • Dead zone settings: 5 microseconds.
  • Operating voltage: 3.5V ~ 6V
  • Size: 23x12.2x12.2x29mm
  • Weight: 13g
  • Function: For Boat / Car / Aircraft / Helicopter / Robot
  • Servo: Analog servo.

The micro servo also includes some arms to test its operation as you can see in the picture.


Testing servo on Arduino UNO



The micro servo I used has 3 pinouts. One is for GND, one for VCC and the last one for the PWM signal. These pinouts are already connected to the servo.


Before testing the servo motor on my week 8 board, I tested it on an arduino I have at home. I did this in order to prevent my board from breaking down. This way I test the servo motor operation without affecting my board.

The arduino I used is an Arduino UNO, I have this one for testing.


To know the connections the Arduino has well defined labels on the board to know which pins to use. The following image is the pinout of the Arduino UNO.


This was the connection I made, it was actually quite easy. To test it, I made a code that basically what it does is to move the servo from 0 to 180 degrees and from 180 to 0.


Here my code:


						// Include the Servo library to control the servo motor.
						#include <Servo.h>
					
						// Create a servo object named myServo for controlling a servo.
						Servo myServo;  
					
						// Attach the servo on pin 9 to the servo object for PWM control.
						void setup() {
						  myServo.attach(9);  
						}
					
						// Move the servo from 0 to 180 degrees
						void loop() {
						  // Goes from 0 degrees to 180 degrees
						  for(int angle = 0; angle <= 180; angle += 1) { 
							myServo.write(angle);              // Tell the servo to go to the position in angle.
							delay(15);                         // Wait 15ms between each position to slow down the movement.
						  }
						  
						  // Move the servo from 180 to 0 degrees
						  for(int angle = 180; angle >= 0; angle -= 1) { // Goes from 180 degrees back to 0 degrees
							myServo.write(angle);              // Tell the servo to go to the position in angle.
							delay(15);                         // Wait 15ms between each position to slow down the movement.
						  }
						}
					

Result


SG90 and Xiao ESP32 Servo Pinout



The microcontroller I used in week 8 is a Xiao Esp32C3. For the PWM signal I can use any pinout from D0 to D10. And for the voltage I used 5V.


To find out the connections on the board, check the design I made in KiCad. I used pin 2 for GND, pin 3 for VCC and pin 7 as my PWM signal pin. You can see these pinouts below the Xiao Esp32. That was the configuration I did in week 8. But it is important to know it to make the correct connections.


Test Run: Code Implementation and Testing



To test the servo operation, I made a code in Arduino IDE. To test it I had to install a library in the library manager. The library is called ESP32 Servo. This library is made for the microcontroller I used, since the Servo library does not work for the Xiao Esp32.


The code I made positions the servo at 90 degrees as the starting angle. Instead of using a potentiometer I used the two buttons I had designed in week 8. When I press one button the servo turns at 0 degrees and when I press the other button the servo turns in the other direction at 180 degrees.


							// Include the ESP32Servo library
							#include <ESP32Servo.h>
						
							// Define pin connections
							const int servoPin = D3;   // Pin connected to the servo
							const int buttonPin1 = D0; // Pin connected to the first button
							const int buttonPin2 = D10; // Pin connected to the second button
						
							// Create a Servo object
							Servo myServo;
						
							// Initial servo angle and increment for button press
							int angle = 90; // Initial angle of the servo
							int increment = 3; // Amount to increase/decrease the angle when a button is pressed
						
							// Variables for debouncing
							unsigned long lastPressTime = 0; // Time when a button was last pressed
							const unsigned long debounceDelay = 50; // Debounce delay to prevent multiple detections
						
							// Setup configuration
							void setup() {
							  pinMode(buttonPin1, INPUT_PULLUP); // Initialize the first button pin as an input with internal pull-up resistor
							  pinMode(buttonPin2, INPUT_PULLUP); // Initialize the second button pin as an input with internal pull-up resistor
							  myServo.attach(servoPin); // Attach the servo on the servoPin to the Servo object
							  myServo.write(angle); // Initialize the servo to its initial position
							}
						
							// Main program loop
							void loop() {
							  // Check for button press with debouncing
							  if (millis() - lastPressTime > debounceDelay) {
								// If the first button is pressed, move the servo in one direction
								if (digitalRead(buttonPin1) == LOW) {
								  if(angle < 180) { // Check if the angle is less than 180 degrees
									angle += increment; // Increase the angle
									myServo.write(angle); // Move the servo to the new angle
								  }
								  lastPressTime = millis(); // Update the lastPressTime
								}
						
								// If the second button is pressed, move the servo in the opposite direction
								if (digitalRead(buttonPin2) == LOW) {
								  if(angle > 0) { // Check if the angle is greater than 0 degrees
									angle -= increment; // Decrease the angle
									myServo.write(angle); // Move the servo to the new angle
								  }
								  lastPressTime = millis(); // Update the lastPressTime
								}
							  }
							}
						

Result