Outils pour utilisateurs

Outils du site


issue104:tutoriel1

Ceci est une ancienne révision du document !


Table des matières

1

By the time you read this, it will probably be old news that there is a new Raspberry Pi that was released on November 26, 2015. It's called the Raspberry Pi Zero and the price is an unbelievable $5 U.S. or 4₤. I haven't had a chance to find any actual dimensions, but they say it is about the size of a stick of gum. So if you've been holding off getting your new Pi due to cost, now you don't have an excuse. We will discuss the Pi Zero in future articles. Now back to my Physical Programming series. This time we are going to start actually controlling things. Hopefully, you have been able to procure some LEDs, resistors, switches, jumpers and a breadboard. As we go through the series, I will be using a free design tool called Fritzing to provide a visual representation of what the project wiring should look like. You might want to get yourself a copy from their website (http://fritzing.org/home/). Not only can you keep copies of our projects locally, you also can have some fun designing your own circuits.

2

A Quick Discussion Of Our Components One more thing before we get started, which is a quick discussion on some of the electronic components we will be using this time, Resistors, LEDs and Switches. Resistors A resistor is a device that 'resists' the flow of electricity to a given extent. This will allow us to limit the amount of electricity that flows through a circuit or part of one. In the case of the LED projects, we will be using resistors so that they will reduce the amount of electricity flowing through the LED (and the GPIO pin), to keep it from burning out. For a more detailed discussion of resistors, please see: https://learn.sparkfun.com/tutorials/resistors. LEDs LEDs are Light Emitting Diodes and are the “standard” replacements for bulbs in just about everything. With a little care in design, they will last almost forever. An LED has two leads/wires called Anode and Cathode. The Anode is the positive side and the Cathode is the negative side. If you have a new LED directly out of the package, you will notice that one of the leads is longer than the other. That is the Anode or the positive side. If both leads on a new device are the same length (or if you are recycling parts from an old circuit board), look for the flat edge. That will always show the Cathode or negative lead.

3

Switches The switch I chose to use for this project is one that easily mounts in the breadboard or on a circuit board. It is simply square with a small round momentary button on the top. It also has 4 pins. The trick is to know which two pins of the four will be the ones we need. You could take an ohm-meter and run across all the combinations of pins until you find the set that works, or you could just look at the layout of the pins that connect it to the breadboard. The two pins to be used have the leads that grip into the board pointing at each other. You only need one set of pins, so just pick the set you wish. Our First Project… Now let's get started with our first construction project. It's a very simple electronics version of “hello world”. We will connect a switch to one of the GPIO pins and monitor it to catch the press of the button. Shown right is the actual schematic that we will be working with. So we have a switch that is connected between ground pin and GPIO pin 17 which is physical pin 11. We also have an LED connected with its cathode to GPIO pin 18 (physical pin 12) and its anode connected to a resistor that connects to the 3.3volt pin on the Pi. It is at this point that you need to make a decision. Will you reference the pins by their position on the board, or the GPIO numbers. We'll get back to that in a minute. In the meantime, here's the wiring diagram…

4

You can see on the breadboard the three components…the switch, the LED and the resistor. The first pin on the RPi is the one on the top right. That pin provides the 3.3 volts DC that we need to power our project. The pin below it is counted as pin #2. Pin #6 is a ground pin. Note that both of those pins have wire connectors that go to the long horizontal buses on the breadboard. Some breadboards have a “+” and “-” on the power bus to help you remember which bus is which. I also have a long jumper from the positive 3.3 volt bus at the top of the breadboard down to the bus on the bottom. It really doesn't matter which bus on the breadboard you use for your power, as long as you are consistent. There is a short jumper going from the top ground bus to one side of the switch and the other side of the switch connects to physical pin 11 on the RPi (or GPIO pin 17). As for the LED, the Cathode is connected to the physical pin 12 on the RPi (GPIO 18) and the Anode is connected to the resistor, which in turn is connected to the lower 3.3 volt bus. Also notice that the wiring is colour coded. Red will ALWAYS (in my diagrams) be a positive voltage, Black is for ground. Any other colors will mean interconnections for data.

5

If you have been keeping up so far, you will notice that I am giving both the physical pin number as well as the BCM GPIO pin number. The “BCM” stands for Broadcom, and, in our code, we will have to tell the RPi.GPIO library if we are using board numbering or BCM numbering. This is the decision I was referring to earlier. In our code, we will have to be consistent with one numbering scheme or the other. In the code we are about to look at, I provide both, and you can comment out whichever one you don't want to use. My personal preference is to use the BCM GPIO numbers, but for this project, I will stick with the physical board pin numbers. Now let's get into the code. As always, I will break the code into parts and discuss each one. First (top right) we have to import the RPi.GPIO library, and we will alias it to the name “GPIO” to make things easier to type. Next, we define two variables; LedPin and BtnPin to the pin numbering scheme we wish to use. Here, I've decided to use the Physical pin numbering, since you probably don't have a breakout wedge yet. I've found the one from SparkFun to be very nice, but it gives you only the BCM numbers on the pins. Our next bit of code (shown below) will be a function called “setup”, where we set up the information for the library to use. Notice that the first line is commented out since I will be using the board numbering in this example, but it's there to show you how to make the call.

6

Lines 3 and 4 show how to define what the pins will be, either input or output, and if we use the internal pull up resistors built-in on the RPi or not. So basically this portion of the code says to use physical board pin numbers as references, and it defines the Output pin to drive the LED and the pin that the signal from the button will be coming in on. Also notice that we define the pin for the button to have a pull-up resistor. This means that the signal-line will be at 3.3 volts and when the button is pressed it is pulled down to ground. Our next function (following page, top right) is called loop, and, as the name suggests, we simply do a loop, checking the button input pin to see if it has been pulled low. If it has, then we turn the LED on, otherwise we set the LedPin to high. That might sound counter-intuitive, but remember we have the Anode connected to the 3.3 volt bus through the resistor. This means to turn the LED on, we have to pull the Cathode down to ground (0 volts) level to allow the LED to turn on. The destroy function (shown below) basically cleans up the states of the pins so we don't get any errors the next time we need to use them. Finally, we use the “main loop” (shown bottom) to call the routines in the proper order and to allow us an easy way to break out of the loop by pressing the Ctrl-C key sequence.

7

So, load the program into your RPi and run it. You will notice the text “…LED Off” keeps repeating on the screen until you press the button. That is due to the fact that our loop routine reads the level or status of the button pin and once the voltage goes low, it says “oh…the button input is low, so I need to turn on the LED”. One other thing to notice is that our first and second routines are named “setup” and “loop”. It is a good thing to keep this format, because when we get to the Arduino programming, these two routines are required. We are going to stop here for this month. I want the other authors to have room for their articles. Keep everything close at hand, because we will be using the same hardware setup next time. Enjoy playing for now and I will see you next month.

issue104/tutoriel1.1452019327.txt.gz · Dernière modification : 2016/01/05 19:42 de fredphil91