Controlling Servo Motor (Sg90) With Raspberry Pi 4
by ogonyesolomonoche in Circuits > Raspberry Pi
3607 Views, 2 Favorites, 0 Comments
Controlling Servo Motor (Sg90) With Raspberry Pi 4
This tutorial gives an introduction to robotics , it involves controlling servo motor with raspberry pi 4 microcontroller.
APPLICATIONS
Further applications of servo motor can be used to create complex system like
- Robotic Arms (Industrial Robots)
- Humanoid Robot
Supplies
HARDWARES
- Raspberry pi 4
- Servo Motor sg90
- Jumper Wires ( male-female )
- Raspberry pi 15W USB-C power supply
- Micro SD card
- PC Computer
Setting Up the Raspberry Pi
Use this link here for easy guide for setting up the microcontroller or watch the video.
Hardware Setup
After setting up the microcontroller and powering it ON. Please FOLLOW the circuit diagram for easy connecting of the hardware
Programming
Open the built-in python IDE (Thonny) or use this link here as guide. You can aslo use this link
here to get the source code from my github account.Run the CODE below on the Thonny IDE:
# We imports the GPIO module
import RPi.GPIO as GPIO
# We import the command sleep from time
from time import sleep
# Stops all warnings from appearing
GPIO.setwarnings(False)
# We name all the pins on BOARD mode
GPIO.setmode(GPIO.BOARD)
# Set an output for the PWM Signal
GPIO.setup(16, GPIO.OUT)
# Set up the PWM on pin #16 at 50Hz
pwm = GPIO.PWM(16, 50)
pwm.start(0) # Start the servo with 0 duty cycle ( at 0 deg position )
pwm.ChangeDutyCycle(5) # Tells the servo to turn to the left ( -90 deg position )
sleep(0.5) # Tells the servo to Delay for 5sec
pwm.ChangeDutyCycle(7.5) # Tells the servo to turn to the neutral position ( at 0 deg position )
sleep(0.5) # Tells the servo to Delay for 5sec
pwm.ChangeDutyCycle(10) # Tells the servo to turn to the right ( +90 deg position )
sleep(0.5) # Tells the servo to Delay for 5sec
pwm.stop(0) # Stop the servo with 0 duty cycle ( at 0 deg position )
GPIO.cleanup() # Clean up all the ports we've used.