Ceci est une ancienne révision du document !
Welcome back to our continuing journey to control our world using Python, the Raspberry Pi, and microcontrollers like the Arduino Uno. In this leg of our journey, we will be learning the basics of the Arduino.
This requires us to have a basic understanding of the Arduino and the Arduino programming language, which, unfortunately, is NOT python, but based on the C language. The good news, however, is that it won't take you long to get the hang of it, and we won't have to get too deep into it for the things we will be doing.
The Arduino Microcontroller
The Arduino (quoting from the Arduino.cc website) is an open-source electronics platform based on easy-to-use hardware and software. It was released as open-source hardware and software, meaning that the plans and parts list are available for you to make your own Arduino from scratch.
Basically, it allows you to read inputs and control output pins on the board in a process very similar to the GPIO pins on the Raspberry Pi.
You can get the genuine Arduino boards for around $35 USD, but there are good clones out there (especially the Sparkfun Red Board) for around $20 USD.
There are two boards that I would suggest for our projects. The UNO and the MEGA. shown top right is a breakdown of the two.
Depending on your desire to go beyond our basic projects, you might want to consider going with the Mega board.
Bottom right is an image of a “standard” (albeit dusty) Arduino Uno board.
Open the Arduino IDE and select File | Examples | 01.Basics | Blink . This will cause another window to open with the new source code. I've copied it here (next page) so we can discuss this very simple project.
Of course, the first thing you will notice is that the source code is in the C programming language. Not to worry, it's pretty simple for what we will be doing.
The part at the top is a block comment which starts with the “/*” and ends with the “*/”. Like Python, the C compiler ignores these block comments. The lines that start with “” are single inline comments and can be put anywhere in code. However, once the compiler sees the “”, everything else on that line is ignored.
There are two functions that must be included in every project… the setup function and the loop function. You can write your own, but there must be these two. The setup function runs every time the board is powered on, or the reset button is pressed. Once the setup function finishes, the loop function is called (at least in this example), and it runs continuously doing all the steps within it.
In this setup function, we only have to tell the board that pin number 13 will function as an output pin. On the Uno and Mega, there is an onboard LED that is connected to pin 13, so we will be controlling that, as well as the actual pin, and not have to worry about any external components this time.. In the Loop function, we send, via the digitalWrite function, a High signal turning the LED on, wait 1 second (using the delay function), set it back to a low signal turning off the LED, wait another second, and then start the process all over.
Plug your Arduino board into the USB port of your computer and click on the round check-mark button to compile and verify your code. Once it reports that the compile has completed successfully, click on the button next to it (the one with the arrow pointing to the right) to upload your code to the Arduino board. In a few moments, if everything worked well, you should see the LED on the Arduino start to flash on and off in one-second cycles.
If you got any error messages on the upload, check the settings under the Tools menu item. The board needs to be set to the type of board you are using, Port should be the one connected to the Arduino (which is USUALLY sensed for you).
Now let's start to have a little fun modifying this code. The delay function takes one parameter, which is the number of milliseconds to delay. In this case we are waiting 1000 milliseconds, which is one second. Let's change both of the delay calls to 250 milliseconds and see what happens.
delay(250);
Don't forget to end the statement with a semicolon. Click on the check button (re-compile) and then upload to the board.
Hopefully it is now blinking twice a second.
Now let's get a bit more creative. Let's make the board blink in Morse Code the SOS signal. That is three short blinks, a bit of delay, three long blinks, a bit of delay and three more short blinks. I won't try to get fancy at this point, but will simply copy and paste more digitalWrite and delay calls with the appropriate delays. Here is the modified loop function.
Compile and upload the code.
Next time, we will start working with some of the components we used in the earlier Raspberry Pi projects, so grab some LEDs and resistors from your component archives to be ready.
Until then, play with some of the sample projects that you find in the Arduino IDE and have fun.