WEEK9 Output devices
WEEK9 Output devices
Group Assignment
measure the power consumption of an output device
Group assignment Page
Individual Assignment
add an output device to a microcontroller board you’ve designed, and program it to do something
In this assignment, I will control a servo motor connected to a test board I made.
Servo Motor SG90
The SG90 is a servo motor that can rotate approximately 180 degrees and is composed of a DC motor, a positioning system, and gears. It has three wires: the red wire is connected to the power supply, the brown wire is connected to the ground, and the yellow wire is connected to the PWM (Pulse Width Modulation) signal.
Controlling servo motor 1
Download sample program:
File->Examples->Servo->Sweep
I updated the pin number in the source code from 9 to 6 to reflect the servo being connected to pin 6.
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
https://www.arduino.cc/en/Tutorial/LibraryExamples/Sweep
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(6); // attaches the servo on pin 6 to the servo object
}
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 15 ms 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 15 ms for the servo to reach the position
}
}
Controlling servo motor 2
A code to control the rotation amount of a Servo Motor.
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
p = GPIO.PWM(4, 50)
#Duty Cycle 0%
p.start(0.0)
while True:
print("input Duty Cyle (2.5 - 12)")
dc = float(input())
#DutyCycle dc%
p.ChangeDutyCycle(dc)
time.sleep(0.4)
p.ChangeDutyCycle(0.0)
ChatGPT Prompt
Please convert the above code to Arudino programming language.
#include <Servo.h>
Servo myservo; // Create a servo object
void setup() {
myservo.attach(6); // Attach the servo motor to GPIO 6
myservo.write(0); // Set initial position to 0 degrees
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
if (Serial.available() > 0) {
float pos = Serial.parseFloat(); // Read input from serial and convert to floating point
// Clear remaining characters in serial buffer
while(Serial.available() > 0) {
Serial.read();
}
if (pos >= 2.5 && pos <= 12) {
// Manually convert duty cycle to angle
float angle = (pos - 2.5) * (180 - 0) / (12 - 2.5);
myservo.write((int)angle); // Rotate the servo motor to the specified angle
Serial.print("Moved to: ");
Serial.println(angle);
} else {
Serial.println("Input out of range");
}
delay(400); // Wait for 0.4 seconds
}
// For stabilization after movement
myservo.write(0); // Return servo motor to 0 degrees position (if necessary)
delay(100); // A short delay for stabilization
}