Hardware Reference
In-Depth Information
This gpio program is part of the WiringPi package, which can be found at https://projects.drogon.net/
raspberry-pi/wiringpi . We use this package also to help solve the complication of understanding the differences in
pin nomenclature. The WiringPi code allows us to refer to wiring pin 0 as the output of our system, which is mapped to
the physical pin 11 because that's where GPIO pin 17 happens to live. (Now pause and reread that until you understand
the difference between each type of pin, as it will save much pain later.) The odd numbering of the pins is just how this
particular chip is configured. So if we were to port the code to an alternate processor, where GPIO-17 was on physical
pin 9, we could keep our code exactly as it, and simply rebuild the circuit with our LED on the appropriate pin.
Of course, the name “Pi” references the original intention of being programmed primarily in Python. So, if that
language takes your fancy, you would instead write:
import Raspberry Pi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
GPIO.output(11, GPIO.HIGH)
As with the Arduino, and most micro controllers, the Raspberry Pi cannot provide enough current for an
electric motor. (It is recommended that you draw no more than 50mA from any pin, and less than 150mA in total.)
Therefore, you will need to employ a driving circuit with a relay that follows the same design as previously shown in
Figure 8-2 .
The code for input signals, such as from a switch, require the code changes you'd expect:
GPIO.setup(12, GPIO.IN)
inputValue = GPIO.input(12)
allowing you to build the same level of circuits as you might with an Arduino, but also allow you to hardness the power
and connectivity of a full Linux machine.
N To learn more of this style of programming, along with some examples, head along to
http://elinux.org/RPi_Low-level_peripherals .
Tip
Once a motor is connected to the Raspberry Pi you'll need an effective way of controlling it. Depending on the
use, it will depend on the type of motor to buy, although a feedback loop that tells the machine how far the motor has
moved is always recommended.
For cases in which a significant amount of motion is required, such as a curtain rail, then a stepper motor or a
standard DC motor can be employed. In the former case, feedback is automatic because the motor is programmed to
move in a specific number of discrete steps. For a DC motor, either limit switches or slotted disc counters can be used.
If you notice the motors used in LEGO Mindstorms projects, you'll understand how to build this from more solid
components. If your feedback is of a Boolean variety, that is, you only need to know if a limit has been reached, then a
simple microswitch at the extent is all that's necessary.
Alternatively, for light applications such as robots, the best solution is to employ servos where the position of
the motor can be controlled by the pulse width of the output signal. Accurate control of this pulse is vital, and while it
might be difficult programming for the newbie, especially since there is only one PWM pin available, there is already a
library to solve this problem by emulating PWM in software.
 
Search WWH ::




Custom Search