How to control a servo motor using a Raspberry Pi board

How to control a servo motor using a Raspberry Pi board

Hello and welcome back. In this tutorial, we will learn, how to control a servo motor using a Raspberry Pi board. For that, I use the SG90 servo motor and the Raspberry Pi 2 Model B board. It depends on your requirement. Also, potentiometer analog values are used to rotate the servo motor. For that, I used the ADS7830 analog to digital converter. Because the raspberry pi boards don’t have analog input pins. If you don’t have any knowledge of this ADS7830 analog to digital converter, please use this link for that. I think this tutorial knowledge is most important for servo motor-based projects.

(adsbygoogle = window.adsbygoogle || []).push({});

Specifically, I used a GPIO extension board and a GPIO ribbon cable for this project. Those components were very useful for me. But you can do this project without these components. OK, let’s move on.

  • GPIO Extension board
  • GPIO Ribbon cable

  • How to control a servo motor with the Raspberry Pi Pico board — Click me

Ok, let’s do this project step by step. The required components are given below.

Step 1

Firstly, identify these components.

(adsbygoogle = window.adsbygoogle || []).push({});

Step 2

Secondly, place the GPIO extension board on the breadboard. Then, connect the GPIO ribbon cable to the extension board.

Step 3

Thirdly, place the ADS7830 analog to digital converter on the breadboard and connect it to the extension board. For that, use the circuit diagram below.

How to control a servo motor using a Raspberry Pi board

Step 4

And then, connect the potentiometer and servo motor to the extension board.

(adsbygoogle = window.adsbygoogle || []).push({});

Step 5

Now, connect the ribbon cable to the Raspberry pi board. Then, connect the mouse, keyboard, monitor, and SD card (including the operating system) to the Raspberry Pi board.

Step 6

Finally, connect the power supply to the board.

Step 7

Ok, now let’s create the program for this project. For that, follow the steps below.

  • First, enable I2C communication, And then, you can check it using the terminal.

(adsbygoogle = window.adsbygoogle || []).push({});
  • Next, open the Thonny IDE. Then, copy and paste the program below. It is described line by line.
  • The complete program of this project — Download
#Include the library files
import RPi.GPIO as GPIO
from smbus import SMBus
from gpiozero import AngularServo
from time import sleep

bus = SMBus(1)
# Include the servo motor pin
servoPin = 17

# Analog input min and max values
in_min = 0
in_max = 255
# Servo motor min and max degrees
out_min = 0
out_max = 180

# Create an object for the servo motor
servo = AngularServo(servoPin,min_angle=0,max_angle=180,min_pulse_width=0.0005,max_pulse_width=0.0025)

def analogInput():
    value = bus.read_byte(0x4b)
    # Convert analog values from 0 to 180
    value = (value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
    return int(value)

while True:
    value = analogInput()
    # Rotate the servo motor using analog values
    servo.angle = value

  • Now, save this program and click the run button.

OK, enjoy this project. The full video guide is below. So, see you in the next tutorial or project.

How to control a servo motor using a Raspberry Pi board

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *