Raspberry Pi 3 and a Servo
RPi3 and microServer SG90
This project module takes a users input from a command line, between integer values 0 and 180, and moves the servo horns (arms) to the user’s inputted value. In this project I am working with the Raspberry Pi and the RPi.GPIO module. However, there are other modules one can use to interface with the GPIO pins on the Raspberry Pi such as the wiringpi and gpiozero libraries. Additionally, I am using a MicroServo 9G (SG90) servo, a Raspberry Pi B+, and Python3.5.
First, open a terminal to create a project folder (directory) and locate it in your $ home/pi or other file location, for example, $ home/pi/Desktop. This will be the top-level folder (directory) which will house your project’s sub-directories and other files. Then use the Linux command mkdir on the command line to create the folder, like so: $ mkdir <filename_here>. You might need use sudo (super-user-do) at the front of the command line statement if it returns any errors. Then move into that folder directory by using the cd command: $ cd <fileName_here>. Next, using the command touch to create a file (script/module) for your project, enter: $ touch <filename_here> and then give yourself access to that file by entering $ chmod 755 <the_filename> or $ chmod u+x <the_filename> . Now, your setup to use an editor of your choice: vi, vim, nano, gedit, Sublime, Visual Studio, Eclipse, or Python’s IDLE to write the module.
ENTER the code below:
$ import RPi.GPIO as GPIO # statements using the (#) hash are comments only
$ import RPi.GPIO as GPIO
$ GPIO.setmode(GPIO.BCM) # Notice, I set the mode to BCM. Not BOARD.
$ GPIO.setwarnings = False
# a good resource for pin assignment is here.
$ servoPin = 13 # BCM pin 13 is physical pin 33 . This is PWM1.
$ GPIO.setup(servoPin, GPIO.OUT) # set pin as OUTPUT
$ pwm = GPIO.PWM(servoPin, 50) # we use 50Hz
$ pwm.start(6.5) # Initialize arm to Center(90-degrees). Play with it if not ~ 90.
# Some math to explain loop how degrees is evaluated?
# How-to Calculate the Slope of a line
# Slope(m) = ((change over y) / (change x))
# m = (y2 -y1 ) / ( x2 – x1)
# my test measured dutycycles(dc) for Zero degrees / far RIGHT 0 degrees is 2.
# my test measured dutycycles for 180 degrees / far LEFT 180 degrees is 11.
# middle position dc of 5.
# insert values: (degrees, dc=value)
# m = (0,2) and (180, 11), so: (11-2) / (180-0) = 9/180 = 1/20 or 0.05.
# The slope (m) of the line is 0.05.
# now we define the point slope of a line: the point the line passes through
# we know from previous that the slope m = 0.05.
# y – y1 = m(x – x1) Knowing one-point: using here (0, 2) coordinates.
# y – 2 = 0.05 (x – 0)
# y = 0.05(x – 0) + 2 // multiply 0.05 times x and 0.05 times 0.
# y = 0.05x + 2 // x is the value of the degrees we want to enter.
The Loop and Range()
# A for loop using range(start, stop, step) function.
# Be sure to indent the for loop body 4 spaces in order for it to execute at runtime.
for i in range(0, 11):
desiredPosition = float(input( “What degree do you want the servo arm pointed at? -180\n”))
desiredCoords = 0.05 * (desiredPosition + 2.00) # using point-slope of a line formula
# In the terminal enter the following statement to run the program:
$ python (or, python3) <the_name_of _your_module>
# Or in IDLE you can just click the ‘run’ button.