0% found this document useful (0 votes)
8 views11 pages

GPIO

The document provides an overview of using GPIO pins on a Raspberry Pi to control devices like LEDs and servos. It explains the necessary libraries, setup procedures, and programming techniques, including PWM signals for servo control. Additionally, it includes examples for blinking an LED and moving a servo using pygame for input control.

Uploaded by

vinod.vynavin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views11 pages

GPIO

The document provides an overview of using GPIO pins on a Raspberry Pi to control devices like LEDs and servos. It explains the necessary libraries, setup procedures, and programming techniques, including PWM signals for servo control. Additionally, it includes examples for blinking an LED and moving a servo using pygame for input control.

Uploaded by

vinod.vynavin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

GPIO (General Purpose Input Output)

Servo
GPIO setup
Whenever you use the GPIO pins, you have to import two libraries.

RPi.GPIO (imported as GPIO) allows us to send signals through the pins with
python.
Pigpio uses the raspberry pi’s built in hardware timer to create different
frequencies/signals.
Next, we set the pin mode to the default setting with
GPIO.setwarnings just gets rid of a startup message
Controlling a light
Now that we have our pins set up for use, we can use them to control an led.
Once you are done wiring the LED, you can control it by sending a signal to the
assigned pin.
First assign the pin we are using and make sure it is an output:

Then just give it some current through the assigned pin:


Implementation
Try making a program to make the LED blink on and off 5 times. You can use the
following line to delay the next line from running:

0.5 can be substituted for any amount of time (in seconds)


PWM signals
Lets make things move now. Like I mentioned before, the raspberry pi has
hardware built to deliver PWM signals. PWM signals are signals that rapidly turn on
and off. When put through an oscilloscope (a device used to measure frequencies)
they look like this:

The parts that are higher up are on and


the parts that are lower are off. Think of it
as binary data (0s and 1s) 0 is off and 1 is
on. This allows us to send data to the
servo.
Getting started:
To use the pigpiod library, you have to run it in the command prompt first:

Go to the menu open, accessories, click on terminal, then type “sudo pigpiod” and
press enter
Programming PWM to control servos
Make sure you have the proper libraries imported:

Initialize GPIO:

Send a signal:

Serv1ang is a value between 500 and 5000. It is the angle of the servo.
Setting the angle of a servo
To set an angle just use the following line:

Servo1 will be the pin that you connected the orange wire of the servo to, and the
number is a value between 500 and 5000 that control the angle.

To stop the servo you have to run this block:


Controlling with pygame
In this example we use pygame’s input system to rotate the servo with an arrow
key at a set speed:

The if statement creates boundaries that the servo cannot pass. This is the perfect
example of adding to an angle instead of going to a set angle.
Implementation
Make a program to wave a servo back and forth 5 times.

You might also like