Typically as a programmer you will work with data on disk, and if you are lucky you will draw pictures on the screen. This is in contrast to physical computing which allows you as a programmer to work with data sensed in from the real world and to control devices that move in the real world.
Use a Raspberry Pi to read in accelerometer value and to control a servo motor.
- Raspberry Pi
- Small $35 Linux computer with 2 USB ports, HDMI out, Ethernet, and most importantly…
- GPIO Pins
- General Purpose Input/Output Pins
- This is the component that truly enables “physical computing”. You as a programmer can set the voltage high or low on each pin, which is how you will talk to actuators. You can also read what the voltage is currently on each pin. This is how sensors will talk back to you. It is important to note that each pin represents a binary state, you can only output a 0 or a 1, nothing in between.
In this article I will go over four basic Python projects to demonstrate the hardware capabilities of the Raspberry Pi. Those projects are:
- Blink an LED.
- Read a pot.
- Stream data.
- Control a servo.
Blink an LED.
An LED is a Light Emitting Diode. A diode is a circuit element that allows current to flow in one direction but not the other. Light emitting means … it emits light. Your typical LED needs current in the range of 10-30 mA and will drop about 2-3 volts. If you connect an LED directly to your Pi’s GPIO it will source much more than 30 mA and will probably fry your LED (and possibly your Pi). To prevent this we have to put a resistor. If you want to do math you can calculate the appropriate resistance using the following equation:
R = (Vs - Vd) / I
But if you don’t want to do math then pick a resistor between 500-1500 ohms. Once you’ve gathered up all your circuit elements (LED and resistor)
The code is also pretty simple. But first you will need to install RPi.GPIO. (It might come preinstalled on your OS.)
import time from itertools import cycle import RPi.GPIO as io io.setmode(io.BCM) io.setup(12, io.OUT) o = cycle([1, ]) while True: io.output(12, o.next()) time.sleep(0.5)
The important lines basically are:
io.setup(12, io.OUT) io.output(12, 1)
These lines of code setup pin 12 as an output, and then output a 1 (3.3 volts). Run the above code connected to the circuit and you should see your LED blinking on and off every half second.
Read a pot.
A pot is short for potentiometer, which is a variable resistor. This is just a fancy word for knob. Basically by turning the knob you affect the resistance, which affects the voltage across the pot. (
V = IR, remember?). Changing voltage relative to some physical value is how many sensors work, and this class of sensor is known as an analog sensor. Remember when I said the GPIO pins can only represent a binary state? We will have to call in the aide of some more silicon to convert that analog voltage value into a binary stream of bits our Pi can handle.
That chunk of silicon is refered to as an Analog-to-Digital Converter (ADC). The one I like is called MCP3008, it has 8 10-bit channels, meaning we can read 8 sensors values with a resolution of 1024 each (2^10). This will map our input voltage of 0 – 3.3 volts to an integer between 0 and 1023.
For more detail: Raspberry Pi Sensor and Actuator Control
Current Project / Post can also be found using:
- raspberry pi linear actuator control