Controlling Servo Motor (Sg90) With Raspberry Pi 4

by ogonyesolomonoche in Circuits > Raspberry Pi

1955 Views, 2 Favorites, 0 Comments

Controlling Servo Motor (Sg90) With Raspberry Pi 4

Raspberry-Pi-Servo-Motor-Control-Image.jpg
idustrial robot.jpg
robot arm.jpg
humaniod robot.jpg

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

raspberry pi 4.jpg
Servo Motor.jpg
rasp adapter.jpg
sd card.jpg
laptop.jpg
jumper wires.jpg

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

rasp os 2.jpg
rasp os.jpg
How to install and set up raspberry pi OS into SD card || Pi4 desktop Step

Use this link here for easy guide for setting up the microcontroller or watch the video.

Hardware Setup

servo and rasp4.jpg
raspberry_pi_3_model_b_plus_gpio.jpg

After setting up the microcontroller and powering it ON. Please FOLLOW the circuit diagram for easy connecting of the hardware

Programming

thonny ide.jpg
thonny ide2.jpg

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.

Downloads

References

Get Started with Thonny IDE on Raspberry Pi

Use this link here to the page.



How To Control A Standard Servo With Raspberry Pi

Use this link here to this tutorial.