Chapter 4. Quickstart

It’s always better—and definitely more fun—to get some practical experience rather than just read about things. This chapter is designed to get you up and running using a solderless breadboard with some electronic components. You’ll learn how to control an LED and then a motor from your Arduino or Raspberry Pi.

Solderless Breadboard

In most cases, when connecting external electronics to a Raspberry Pi or Arduino, it is not possible to connect the device (say, a motor) directly to the Raspberry Pi or Arduino. You will need to connect some extra electronic components to make it all happen. In any case, even if you are just lighting an LED, you will need some way of attaching the LED to your Raspberry Pi or Arduino.

A great way of connecting things up without the need for soldering is to use a solderless breadboard. The breadboard (as it’s usually called) was invented as a tool for electronics engineers to prototype their designs before committing the projects to a more permanent soldered form. Breadboards allow you to experiment with electronics and make your own projects without the need for any soldering.

Figure 4-1 shows a breadboard populated with the components from “Experiment: Controlling a Motor”.

You can see how the breadboard is useful both to hold and connect components and wires.

The breadboard used throughout this book is often referred to as a half-sized breadboard or 400-point breadboard (because it has 400 holes in it). There are bigger and smaller breadboards available, but this size is about right for all the projects and experiments in this book.

Figure 4-1. Using a solderless breadboard

This type of breadboard has two columns on each side of the breadboard. These are usually marked with red and blue lines. For any one of these columns, all the holes in that column are connected together electrically behind the plastic of the board. Although these columns can be used for anything, they are most often used for the positive and negative supply to your circuit.

The main body of the board is split into two banks, or rows, of five holes each down the whole length of the board. Each of these rows of five holes has all five holes connected together by a clip behind the plastic. To connect the leg of one component to another, both leads just need to be pushed into holes on the same row of the breadboard.

How a Breadboard Works

Behind the holes in the front plastic face of a breadboard, there are metal clips designed to grip wires and component leads. 

Figure 4-2 shows a dismantled breadboard with one of the clips removed, so that you can see how it works behind the plastic. I would not recommend taking your breadboard apart like this, as generally it will never quite be the same again once you have put it back together.

Figure 4-2. The inner workings of a breadboard, with one clip removed

Connecting a Breadboard to the Arduino

The Arduino GPIO connections (confusingly often called pins) are really sockets.  To connect one of these pins to one of the rows of a breadboard, you can use a “male-to-male” jumper wire, as shown in Figure 4-3.

Figure 4-3. Connecting the Arduino to a breadboard

These jumper wires have a flexible wire with a little plug on each end. It’s a good idea to keep a good stock of such wires in different lengths ready to connect things up.

You can buy jumper wire packs (see Appendix A) in a variety of sizes and colors. It is well worth getting yourself a little kit of breadboard and assorted jumper wires. Different colors make it easier to see how the wires are connected up, especially when you have a whole load of wires on the breadboard.

Connecting a Breadboard to the Raspberry Pi

The GPIO connection pins of a Raspberry Pi are actually pins rather than sockets. This means that you cannot use the male-to-male jumper wires that you did for the Arduino. Instead, you need to use (you guessed it) female-to-male jumper wires. These have a socket on one end that fits over a pin on the Raspberry Pi’s GPIO header, and a pin on the other end of the lead that fits into the breadboard.

Figure 4-4 shows how you can make connections from the GPIO pins of a Raspberry Pi to a particular row of your breadboard using female-to-male jumper wires.

Figure 4-4. Connecting a Raspberry Pi

Identifying GPIO Pins on a Raspberry Pi

The GPIO pins on a Raspberry Pi are not labeled on the board, so to save you from the trouble of carefully counting pins, you can use a GPIO template to fit over the GPIO connector. The one shown in Figure 4-4 is a Raspberry Leaf, available from Adafruit and MonkMakes.com. Other templates are also available.

Downloading the Software

All the software for this book, both Arduino sketches (as Arduino programs are called) and  Python programs for the Raspberry Pi, are available from the book’s GitHub repository.

You can find instructions for uploading the Arduino sketches from your everyday computer to your Arduino in “The Book Code” in Chapter 2.

To get the Python programs for the book onto your Raspberry Pi, follow the instructions in “The Book Code” in Chapter 3

Experiment: Controlling an LED

In the Arduino world, it has become traditional to, as a first exercise, make an LED blink on and off. In this first experiment, you will also make an LED blink, first using an Arduino and then with a Raspberry Pi. 

This is a nice, easy project to get you started. There are just two components to fit onto the breadboard: an LED and a resistor. All LEDs need a resistor to limit the current flowing through them. You will find more in-depth information about this in Chapter 6.

Parts List

Whether you are using a Raspberry Pi or Arduino (or both), you are going to need the following parts to carry out this experiment:

Part Source
Red LED

Adafruit: 297

Sparkfun: COM-09590

470Ω 1/4W resistor Mouser: 291-470-RC
400-point solderless breadboard Adafruit: 64
Male-to-male jumper wires (Arduino only)

Adafruit: 758

Female-to-male jumper wires (Pi only) Adafruit: 826

This table lists the supplier and product code for each part. You will also find more information about all the parts used in this book in Appendix A.

Breadboard Layout

The breadboard layout for this project is shown in Figure 4-5 This is the same whether you are using an Arduino or a Raspberry Pi, but the way that you link the breadboard to the Raspberry Pi or Arduino will be different.

Figure 4-5. The breadboard layout for controlling an LED

Figure 4-5 shows how the current from an Arduino or Raspberry Pi output pin will flow first through the resistor and then the LED to make it light.

It does not matter which way around the resistor goes, but the LED must have the positive lead toward the top of the breadboard. The positive lead of an LED is slightly longer than the negative lead. Also, the edge of the LED next to the negative lead will have a flat edge.

Resistors will be covered in more depth in Chapter 5.

Arduino Connections

Connect the  Arduino sockets GND and D9, as shown in Figure 4-6. You can see the actual Arduino and breadboard in Figure 4-7.

Figure 4-6. The breadboard layout for controlling an LED with Arduino
Figure 4-7. Using a breadboard with Arduino

Arduino Software

The Arduino sketch can be found in the arduino/experiments/on_off_control directory, which you’ll find in the place where you downloaded the book’s code (see “Downloading the Software”).

The program will turn the LED on for 5 seconds, then off for 2 seconds, and then repeat. Here is the full code:

const int controlPin = 9; // 1

void setup() {  // 2
  pinMode(controlPin, OUTPUT);
}

void loop() {  // 3
  digitalWrite(controlPin, HIGH);
  delay(5000);
  digitalWrite(controlPin, LOW);
  delay(2000);
}
1

The first line defines a constant called controlPin as pin 9. Although the Arduino Uno has digital pins 0 to 13 and analog pins 0 to 5, the convention is that if the Arduino code refers to just a number by itself (in this case, 9) then that refers to the digital pin. If you wish to refer to one of the six analog pins, you must prefix the pin number with the letter A.

2

The setup() function specifies the pin as being a digital output using pinMode

3

The loop() function, which will repeat indefinitely, first sets the controlPin (9) high (5V) to turn the motor on. It then delays for 5,000 milliseconds (5 seconds) and then sets the controlPin low (0V) to turn off the motor. The next line then delays for another 2 seconds before the loop starts again.

Arduino Experimentation

Upload the program to your Arduino. As soon as the Arduino restarts as part of the upload process, it will be running your code and the LED should start blinking. 

If the LED does not blink, then check all the connections and that the LED is the right way around (longer LED lead to the top of the breadboard). 

Try altering the numbers in the delay functions to alter how long the LED stays on on for each cycle. You will need to upload the program again each time you make a change to it.

Raspberry Pi Connections

Unlike the Arduino, a Raspberry Pi does not have any labels next  to its GPIO pins to tell you which is which. This leaves you with two options: you can use a diagram of the GPIO pinout (see Appendix B) and count down to find the pin you need, or you can use a pin identification template that fits over the GPIO pins, such as the Raspberry Leaf shown in Figure 4-8. Figure 4-9 shows the breadboard layout and connections to the Raspberry Pi.

Figure 4-8. Connecting a breadboard to a Raspberry Pi
Figure 4-9. The breadboard layout for controlling an LED with a Raspberry Pi

Raspberry Pi Software

You do not need a separate computer to program the Raspberry Pi—it’s possible to write and run the program on the Raspberry Pi itself. To do so, use the following program (you can find it in the on_off_control.py file  in the python/experiments directory):

import RPi.GPIO as GPIO  # 1
import time              # 2

GPIO.setmode(GPIO.BCM)   # 3

control_pin = 18           # 4
GPIO.setup(control_pin, GPIO.OUT)


try:               # 5
    while True:       # 6
        GPIO.output(control_pin, False) # 7
        time.sleep(5)                 
        GPIO.output(control_pin, True) 
        time.sleep(2)                 
        
finally:  
    print("Cleaning up")
    GPIO.cleanup()

The program is quite similar to its Arduino counterpart:

1

To gain access to the GPIO pins of the Raspberry Pi, there is a Python library called RPi.GPIO created by Raspberry Pi enthusiast Ben Croston. The first line of code imports this library so that it can be used in your program. The RPi.GPIO library comes preinstalled in all recent versions of the standard Raspbian distribution, so you shouldn’t need to install it, unless you’re using an old version of Raspbian. In that case, the easiest way to install it is to update your system, which you should probably do anyway, by issuing the following command in a terminal session:

$ sudo apt-get upgrade
2

The time library also needs to be imported, as this is used to cause the delays between turning the LED on and off.

3

The GPIO.setmode(GPIO.BCM) line must be included in every Python program that you write that controls GPIO pins, before you set the mode of the pins or use them in any way. The command tells the GPIO library that the pins are to be identified by their Broadcom (BCM) names rather than by the position of the pins. The RPi.GPIO library supports both naming schemes, but the Broadcom naming convention is the more popular of the two, and it’s the one used throughout this book.

There are no separate setup() and loop() functions used when programming Arduino; instead the things that would go in setup() just appear near the start of the program and a while loop that continues forever takes care of what would normally be in loop() in an Arduino.

4

The variable control_pin identifies GPIO pin 18 as the pin that you are using to control the LED and then this pin is defined as being an output using GPIO.setup.

5

Now you come to the equivalent of the Arduino’s loop function. This is contained in a try/finally construction. The point of this is that if any error occurs in the program, or you just stop the program by pressing Ctrl-C in the terminal window where you ran the program, then the cleanup code in the finally block will be run.

You could omit this code and just have the while loop by itself, but the cleanup code automatically sets all the GPIO pins back to a safe state as inputs, making it much less likely that an accidental short or wiring error on your breadboard could damage your Raspberry Pi.

6

The while loop has a condition of True. This may seem strange, but it’s just the way you make some code continue indefinitely in Python. In fact, the program will just keep looping around the commands inside the while loop until you kill the program with Ctrl-C or unplug your Raspberry Pi.

7

Inside the loop, the code is very similar to the Arduino counterpart. The GPIO pin is set to True (high) then there is a delay of 5 seconds, then the GPIO pin is set to False (low) and another 2-second delay happens before the cycle starts again.

Raspberry Pi Experimentation

To access the GPIO pins, Linux superuser privileges are required. To run your program, change into the directory containing on_off_control.py and then run the program using this command:

$ sudo python on_off_control.py

Including sudo at the beginning of the command allows it to be run as a superuser. When you have had enough of the motor running, press Ctrl-C to quit the program.

Comparing the Code

The overall structure of both Arduino C and Python code is quite similar, but the code itself is different in style. Also, when naming variables or functions, C uses the convention (that is, each word after the first word is capitalized), whereas Python uses snake_case (that is, words are separated by underscores).

Table 4-1 compares some of the key differences between the two programs.

Table 4-1. Arduino C and Python
Command Arduino C code Python code
Define a constant for a pin const int controlPin = 9; control_pin = 18
Set a pin to be an output pinMode(controlPin, OUTPUT)

GPIO.setup(control_pin, GPIO.OUT)

Set an output high digitalWrite(controlPin, HIGH); GPIO.output(control_pin, True)
Set an output low digitalWrite(controlPin, LOW); GPIO.output(control_pin, False)
Delay for 1 second delay(1000); time.sleep(1);

Experiment: Controlling a Motor

Now that you can use the Raspberry Pi and Arduino to turn an LED on and off, let’s apply that knowledge so that you can use them to turn a motor on and off. This will use the exact same software as “Experiment: Controlling an LED”, but you will use a transistor to switch the DC motor.

You will learn a lot more about DC motors in Chapter 7. DC motors are the kind of small motor that you might find in a hand-held fan or a toy car. They are probably the easiest type of motor to use—you supply them with a voltage across their two terminals and their shaft rotates.

Because nearly all motors require too much current to be driven directly from the digital output of the Raspberry Pi or Arduino, a transistor is used to allow a small current from the Raspberry Pi or Arduino to control a much bigger current to the motor.

The same electronics hardware is used for both the Raspberry Pi and the Arduino, and in both cases is built on the same solderless breadboard. 

This is a quickstart chapter, and as such, some of what is going on in this experiment will not be explained until later chapters. The layout also has a lot more components than the first experiment, so take care to ensure that all the component leads are in the right holes and that components that need to be the right way around are correct.

Parts List

Whether you are using a Raspberry Pi or Arduino (or both), you are going to need the following parts to carry out this experiment:

MPSA14 Darlington transistor Mouser: 833-MPSA14-AP
1N4001 diode

Adafruit: 755

Sparkfun: COM-08589

Mouser: 512-1N4001

470Ω 1/4W resistor Mouser: 291-470-RC
Small 6V DC motor 

Adafruit: 711

6V (4 x AA) battery box Adafruit: 830
400-point solderless breadboard Adafruit: 64
Male-to-male jumper wires

Adafruit: 758

Female-to-male jumper wires (Raspberry Pi only) Adafruit: 826

If you are planning to try this experiment with a Raspberry Pi, you will need female-to-male jumper wires to connect the Raspberry Pi GPIO pins to the breadboard.

Breadboard Layout

The breadboard  layout for this project is shown in Figure 4-10.

When you put the components onto the breadboard, you need to make sure that the transistor is positioned correctly—the flat side with the writing on it should be on the righthand side. In addition, you need to check the placement of the diode—it has a stripe at one end that should face the top of the board.

Because playing with something is often more fun than understanding the details of how it works, we won’t fully discuss how this experiment works until Chapter 5.

Figure 4-10. The breadboard layout for controlling a motor

Experimenting Without Arduino or Raspberry Pi

Before you start connecting the breadboard to an Arduino or Raspberry Pi, you can experiment with it and test it out.

The transistor is acting as a switch (more on that in Chapter 5). Eventually, two wires will be connected to the Arduino or Raspberry Pi, GND and Control. The GND (ground) connection represents zero volts for both the breadboard circuit and the Arduino and Raspberry Pi. The Control connection will turn the motor on if it is connected to any voltage over about 2V and the motor will be off if that voltage is less than that.

You can try that out using a male-to-male jumper wire before getting an Arduino or Raspberry Pi involved. Attach one end to the same row as the left lead of the resistor and touch the other end to the top lead of the diode which is also connected to battery + (Figure 4-11). When you do this, the motor will start, and when you take the header lead away from the diode lead, the motor should stop again.

Figure 4-11. Testing the circuit before connecting to an Arduino or Raspberry Pi

Arduino Connections

Now that you are sure that the control lead from the breadboard will indeed turn the motor on and off, you can connect it to one of Arduino GPIO pins using a male-to-male jumper wire. Use the connection labeled “9” on the Arduino, as shown inFigure 4-12. Note this is the same control pin that you used for the LED in “Experiment: Controlling an LED”.

You will also need to connect the other GND lead to the Arduino GND leads. This is also shown in Figure 4-12.

Figure 4-12. The breadboard layout for controlling a motor with an Arduino

Arduino Experimentation

If you still have the program on your Arduino from “Experiment: Controlling an LED”, you do not need to upload anything. If you no longer have that program on your Arduino, follow the instructions in that section so that you can upload it again.

As you did with the LED, try altering the numbers in the delay functions to alter how long the motor remains on for each cycle.

Raspberry Pi Connections

You can now disconnect the breadboard from the Arduino and connect it to the Raspberry Pi. Connect  GPIO pin 18 (the control pin) and GND (ground), as shown in Figure 4-13.

Figure 4-13. The breadboard layout for controlling an LED with a Raspberry Pi

Raspberry Pi Experimentation

The Raspberry Pi program for this experiment is also the same as for controlling the LED.

Run your program, change directory to the directory containing on_off_control.py, and then run the program using this command:

$ sudo python on_off_control.py

When you have had enough of the motor running, press Ctrl-C to quit the program.

Summary

The goal of this chapter was to get you going with actually making something. The next chapter more fully describes the theory behind how this project works. We’ll also discuss how to select the right transistor for the job and identify components. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.217.122