Last time I left you having set up your Arduino to work with Mac or Windows, and having uploaded a simple test app that blinked the on-board LED. Today I’m going to explain the code you uploaded, the structure of Arduino software, and a little more about the electronic bits on the board itself.
This article is part of an introduction to the Arduino series. The other articles in the series so far are:
- What is Arduino and what can you do with it?
- What is an Arduino starter kit and what does it contain?
- More cool components to buy with your starter kit
- Getting Started With Your Arduino Starter Kit – Installing Drivers & Setting Up The Board & Port
The Hardware
Let’s take a closer look at what the Arduino Uno has in terms of bits on the circuit board.
Here’s an enlarged diagram to refer to:
- Along the top, there are 14 digital Input/Output pins (numbered 0-13). These are the most versatile pins on your Arduino and can function as either input or output, and will form the core of your projects. Digital means that the signal these pins can write or read will be on or off.
- 6 of those digital pins, which are marked by the tilde sign ~ are capable of doing what it is called Pulse Width Modulation. I’m not an electrical engineer so I won’t embarrass myself by explaining the science behind this, but to you and I it means we can provide a range of output levels – for instance, dimming an LED or driving a motor at varying speeds.
- Pin 13 is special in that it has a built-in LED. This is for convenience and testing purposes only really. You can use that on-board LED, as you did in the Blink example app, by simply outputting to pin 13 – or it can be used as a standard I/O pin.
- On the bottom right are 6 analog input pins. These will read the value of analog sensors such a light-meter or variable resistors.
- On the bottom left next to the analog input pins are power pins. The only ones you really need to worry about are the ground pins (GND), 3.3v, and 5v power lines.
- Finally, the only switch found on the Arduino is a reset switch. This will restart whatever program it has in its memory.
- The Arduino has a set amount of memory, and if your program goes too large, the compiler will give you an error.
The Structure Of An Arduino Program
Every Arduino program is made up of at least two functions (if you don’t know what a function is, be sure to read my basic programming tutorial, part 2 – function and control statements, and part 1 where we discussed variables before continuing).
The first is the setup function. This is run initially – once only – and is used to tell the Arduino what is connected and where, as well as initialising any variables you might need in your program.
Second is the loop. This is the core of every Arduino program. When the Arduino is running, after the setup function has completed, the loop will run through all the code, then do the whole thing again – until either either the power is lost or the reset switch is pressed. The length of time it takes to complete one full loop depends upon the code contained. You may write some code that says “wait 6 hours”, in which case the loop isn’t going to be repeating very often.
Here’s a quick state diagram to illustrate:
Examining The Blink Program
Take a look back at the Blink program code and identify the setup and loop functions.
Here’s the setup:
void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
}
The lines that begin with // are simply comments to explain the code to a human reader, and they don’t get uploaded to the Arduino. So in fact, there’s only one line of setup code in this particular Arduino app. That line is saying “Set pin 13 to output mode”. 13, remember, is the built-in LED.
Then there is the loop:
void loop() {
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
}
The comments at the end of each line of code explain their function quite well. HIGH and LOW refer to the ON and OFF state of a digital output – in our case the LED. You could actually write ON or OFF in the code too, both are synonymous (as is 0 and 1 also). Delay tells the Arduino to wait for a bit, in this case 1000 milliseconds (or 1 second).
Finally, a note about the programming language used here. Notice that both setup and loop functions have the word void before them. This is a special word for nothing, because the function returns nothing when it is called – it simply runs the code contained within. For now, let’s leave it at that by saying that the function’s block of code is enclosed by curly braces { }, and that each line of code must end with a ; semi-colon.
Try altering the basic program somehow by changing the precise delay values to something larger or smaller. See how small you can get it down to before the flashing is no longer noticeable. Work out which value to change to get it to stay on for longer, or to stay off for longer. Try adding some more digitalWrite and delay statements into the loop function to create a more complex flashing pattern like the morse code for SOS. If you have a buzzer, try connecting it to pins 13 and GND too (hint: the red wire goes to 13, black to ground).
That’s all for today. Next time we’ll add in some more LEDs and write our own application from scratch. As ever, comments and shares much appreciated. I can’t imagine you’d have any problems with the code referred to today, but if you’ve tried adjusting the code slightly and are running into errors or unexpected behaviour, feel free to post it in the comments and we’ll see if we can work through it together.
No comments:
Post a Comment
[Please do not advertise, or post irrelevant links. Thank you for your cooperation.]