Introduction: Controlling Servo Motor (Sg90) With Raspberry Pi 4

About: SO passionate about A.I AND robotics

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

Step 1: Setting Up the Raspberry Pi

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

Step 2: Hardware Setup

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

Step 3: 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.

Step 4: 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.