There's an Arduino for that! |
Written by Harry Fairhead | |||
Tuesday, 16 December 2014 | |||
Page 2 of 2
To flash the Pin 13 LED we are going to write a simple program that first sets Pin 13 to be an output - any pin can either be used as an input or an output and then we are going to write a loop that sets the pin high and then low with a suitable delay. As all embedded hardware programs have the same structure - an initialization portion and a block of code that is repeated forever - the Arduino language has two special functions - setup and loop. The setup function is called once when the program first starts and the loop function is called repeatedly. Basically the the master Arduino program is
In other words the code you place in setup is called once right at the start and never again and the code you place in loop is called repeatedly as fast as possible. In this case we need to put the initialization code in setup and the flashing code in loop. Notice you don't have to write the loop it is created for you. If you run the Arduino editor it opens with a blank "sketch" ready for you to enter some code. The setup function is easy enough - you simply have to set pin 13 to act as an output:
The only thing difficult about this function is knowing that there is a predefined function called pinMode which sets the direction of any pin. The loop function is a little more difficult. You have to set the pin high and then low again to make the LED flash. This is easy as there is a function digitalWrite which can be used to set any pin to HIGH or low. However if you just write:
and run the resulting program all you will see is the LED on all of the time. The reason is that the loop function is called repeatedly as fast as possible and so the LED flashes as fast as possible an in this case it is so fast you can't actually see it flash at all. The solution is to include a delay to keep the LED on long enough and off long enough for the flashing to be obvious. This too is easy as there is a Delay function which will pause the program for the specified number of milliseconds. You might think that :
would do the job but you need to remember the loop is called repeatedly. Yes the LED is switched on then remains on for one second (i.e. 1000 milliseconds) and then it is switched off - but for how long? The answer is that the loop function is called almost immediately and the LED is switched on again. In other words the LED is on for one second but off for a very short time - again you just don't see it flash. The correct version of the program is to make sure that the LED is on for one second and off for one second:
Now if you were to run the program you would see the LED flash at a reasonably slow speed. It is this sort of reasoning about timing and what is happening in the real world that characterises this sort of programming. The complete program is:
You can also find an version of this program as part of the examples provided. If you want to try the example out then simply use the File,Open menu command and navigate to examples/1. Basics/Blink and load the Blink file. To run a program all you have to do is to use the menu command File,Upload to I/O Board or click the upload button: Notice that there is also a Sketch.Verify/Compile command that simply checks that the program doesn't have any errors in it without downloading it and running it. If you do click on the download and run button you will see the LED flash on for one second and off for one second. This is your first program and the rest are just as easy. Where next?After you have reached this point the next question is always what is the next best step? When it comes to embedded programming the answer is complicated by the interplay between the hardware and the software. Your next step is probably to get some simple electronic components - some LEDs, a piezo sounder and some switches. To try things out you also need a solderless protyping board and some pre-made jumper wires to make connections. The basic things to learn are how to flash and LED that isn't on the board and reading the state of a switch. If you want to be more ambitious then look at some of the add on modules - "shields" - that you can get to extend what the Arduino can do. For programmers interested in creating "applications" the most useful are likely to be the Bluetooth, Ethernet, WiFi or XBee modules. But if you are into controlling existing equipment or building robots then the motor, servo or relay shields are likely to be what you are interested in. Remember if you can switch an LED on and off switching something bigger is just a matter of scaling up the output pin! It is worth mentioning that the output pins of the Arduino aren't capable of supply much power and if you want to switch something big on and off you need to "amplify" their output. You can do this with a transistor or a relay and you need to find out how these work. One word of warning stay away from mains voltages until you have a good understanding of electronics. With voltages up to 30V you can destroy electronic components more than 30V and you can damage your self or worse. As far as learning more of the Arduino language is concerned the good news is that it is very simple and very like C, or any modern block-structured language you might know. There aren't many additional commands to learn to control the basic I/O on the Arduino board and these too are very similar to the commands you will find in almost any hardware control language - in other words the skills that you pick up by programming the Arduino are very general. Finally if you want to go further - watch this space over the coming months for more Arduino-based projects and tutorials. Related ArticlesGetting Started with Arduino Book Review Practical Arduino Book Review Robot Xylophone - an Arduino project (Video) Arduino Programming in 24 Hours Arduino And More Comes To MoMA Real Raspberry Pi - Getting Started And Custom NOOBS
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
Comments
or email your comment to: comments@i-programmer.info <ASIN:1449368107> <ASIN:0672337126> <ASIN:0071817727> <ASIN:1783986069> |
|||
Last Updated ( Tuesday, 01 September 2015 ) |