Getting started with an Arduino
I thought I would write a little bit of a blog about the arduino and how to use it for simple tasks. The arduino is very simple 5 volt microcontroller with a very simple programming environment. The best part about the arduino has to be its programming environment, what they call the integrated development environment, or IDE for short. It also has a very simple plain English structure to its commands. Install the IDE here.
Here’s an example of some code.
void loop{
digitalWrite(13, HIGH); // Send 5 volts to pin 13
delay(1000);
digitalWrite(13, LOW); // Send 0 volts to pin 13
delay(1000);
}
To follow along it might be nice to have the arduino reference page up in your browser along side this article. http://arduino.cc/en/Reference/HomePage
Within the IDE in the menus at the top you will find a bunch of examples, how cool is that. There is also a link to the reference page.
Now the thought of writing programming,
for a lot of people sounds way too complicated and a bit beyond what they would like to learn. This is where the arduino has made its mark. First of all it comes with a very simple, easy to install, and easy to use editor. The editor has a menu that includes examples as well as a reference library explaining each command and how to use. The board itself is an amazing piece of hardware based around a very simple microcontroller chip that’s easily replaceable but with the right software it’s incredibly simple to automate tasks and to connect to an amazing array of other hardware.
So lets get started.
Most programming involves the inputting and outputting of information, switch positions, sensor values etc. For example we need to read if a switch is in a given state, we may then decide to turn on a light. So how should we store this information? In variables types such as integers, floats and booleans. Dont worry about the lingo, it will start to make sense after a while. Integers are whole numbers like 42 or 327, float is a decimal type like 3.14159, while boolean can only exist as either TRUE or FALSE. Char is another type which holds characters like the letter T.
Ok so now we have some data types, we need to tell the program about them, give them a name and then think about getting data in and out of these values. Here is an example of “Declaring” an integer;
int x = 42;
we are declaring the variable x as an integer equal to 42. Now we shall put variables aside for later and take a look at some other ways of deciding things and driving pins.
How about this,
void loop{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
Can you hazard a guess as to what it does? You may have noticed the word loop and the {} parentheses at the start and finish, everything inside these parenthese will be executed in order then repeated forever. You may have noticed the command
delay(1000);
All commands end with a semi colon. Delay pauses in milliseconds in this case 1000 which is one second. The rest you should know by now, Turning pin 13 ON, delaying a sec, then turning OFF, then looping around over and over. A blinking light is what we have created.
Now to think about getting data into and out of our micro via the hardware pins so we can actually do some useful work instead of faffing about in software.
Lets look at the following programming statement.
if(digitalRead(pin4)=HIGH){
digitalWrite(pin6, HIGH)
}
You may have already gleaned some information here, you may not. In plain english we can see at least three things, IF, Read, and Write.
The IF command will execute everything that is inside the {} parentheses block whenever the term or equation or whatever is inside the () brackets is either true, equal to 1, or HIGH.. For example;
if(1){
Serial.print(“Hello World”)
}
Would always print “Hello World” to the serial port, whereas;
if(0){
Serial.print(“Hello World”)
}
would skip the parentheses block.
You can open up the serial monitor in arduino, by heading up to the tools menu. Remember to use the serial port, you must first initialize it with the Serial.begin(9600); command.
So, going back to the example;
if(digitalRead(pin4)=HIGH){
digitalWrite(pin6, HIGH)
}
The command digitalRead(pin); returns a HIGH or a 1 if there is 5 volts on the pin number specified.
The code asks IF pin 4 reads HIGH, ie it has 5 volts on it, then digitalWrite 5 volts or a HIGH to pin 6.
So we can connect a button from 5 volts to pin 4 and an LED with a resistor from pin 6 to ground. When we flick the switch the LED will light up. Sounds simple and of course it is.
So now we can read and write to the digital pins, well done.
Straight away we can make all sorts of things, push button operated relays, (morse key?), light chasers and flashers, how about a movement activated light? PIR’s on ebay are about $5 and only need 5v and ground which the arduino can supply, and will output 5volts when triggered, too easy! I have built a board around this.. http://www.tobyrobb.com/shop/index.php?act=viewProd&productId=6&ccSID8643dd60547e611052421a6659ed64d2=cdf37eff4c45604703c8ed7da0d483e1
Now that we have digital coding under our belts, lets have a look at analogue coding. This is going to be quite easy actually, here are the two main commands;
analogRead(pin);
analogWrite(pin);
You’ll notice that the second word in a command is capitalized, that’s so its easy to read the command as two distinct words.
Now in the last example the output of digitalRead() would only return a 1 or a 0, which is all that digitalRead can return.
analogRead is different in that it can return a value, from 0-1023. That’s an integer. if there is exactly zero volts on the pin, we will get back a value of 0, whereas 5 volts would return 1023. 2.5 volts would be 512 and so on, do you get the picture?
To read higher values than 5 volts, we use 2 resistors as a resistor divider.
Now how can we store this value from the analogRead function?
how about this statement;
x = analogRead(A0);
We are reading pin A0 and putting the value into the variable x.
We could do this;
void loop(){
x = analogRead(A0);
Serial.print(x);
}
or this;
Serial.print(analogRead(A0));
both forms would print the results of pin A0, but the first would store it in a variable called x, which we could use later if we wanted too.
How about a simple 0 to 5 voltmeter;
Serial.print(“The voltage on Analog Pin 0 is : ”);
Serial.println(analogRead(A0)/205);
Notice i am dividing the result of the analogRead by 205, to change the value to volts, ie. 1023/205 equals 5 volts or 512/205=2.5 volts.
Another example;
void setup(){
int x = 0;
}
void loop(){
x = analogRead(A0);
if(x>=512){
digitalWrite(13, HIGH);
}
}
If theres more than 2.5 volts on pin A0, then turn on pin 13. When hooked to an LDR or a thermistor we would now have a darkness or temperature activated pin..
While it is light, the LDR is a fairly low resistance, and it pulls the pin down towards zero volts which will not pass our IF statement. As it gets dark, the LDR becomes very high resistance, and the 5v can pass through the 10k resistor raising the voltage on the analog input pin. You may have to fiddle with the values in the code to get the right triggering value, but this is the beauty of using code, you wont need to change the resistors, and you can use the earlier example code to print out the value of the pin so you get an idea of what values you can expect from your sensors.
Well i hope that analogRead wasn’t to hard to understand, just give it a pin number and assign it to a variable with the equals command, or use it directly inside another command.
Now for analogWrite.
Im sure your already guessing what it does right? Yes it puts a voltage on the pin anywhere from 0 to 5 volts, depending on what you tell it. But its not from 0-1023, this time its from 0-255. So it needs to be told which pin to use, and a value. NOT every pin on the arduino can give out analog voltages, just the ones marked with PWM.
The command
analogWrite(10, 255);
Would put 5 volts on the pin, whilst
analogWrite(10, 127);
Would put an average of 2.5 volts on the pin.
Notice i said average. It’s not really a true 2.5 volts, it kinda cheats. It does this by turning the voltage off for half the time and on at 5 volts for half the time. It does this pretty quickly. Giving us an average of 2.5v.
This means that something like an LED, which cant be run on 2.5v and would be very hard to dim, can now be dimmed as its still getting its 5 volts, and will light up, but it is not on for as long as so will appear dimmer, the strobing happens so quickly the eye cannot perceive the flashes.
So thats the basics.
Get yourself an Arduino UNO board, don’t pay more than $15, try ebay worldwide, sort by price and give it a go!
Further experimenting..
There are a huge number of commands available in Arduino to make automating some tasks easier and to make programming quite easy. There are commands to drive servos, stepper motors, LCD’s , serial ports, speakers, LED matrices, even a $10, 0-30mhz@1Volt variable frequency sine wave DDS generator board, all extremely simply. To look at some different hardware, which you probably have in your junk box, try looking here.
Maybe your ready to remove the chip and put it on a breadboard…
There are many commands and many examples, don’t forget you can access the examples and the reference through the menus of the IDE.
This has been a basic walkthrough of some of the common commands, i urge you to try many of the others.
Here is one last example that should be easy to understand and to get you started with what you know already. Just copy and paste into the IDE.
/*
Make some morse with a relay
*/
int relayPin = 13; // This is the integer that tells us what pin the relay is connected to
int dit_time = 100; // This is the integer that tells us how long a DIT is
int dah_time = 300; // This is the integer that tells us how long a DAH is
int space = 100; // The time to wait in between beeps
int letter = 200; // The time to wait in between letters
// Begin setup
void setup(){
pinMode(relayPin, OUTPUT); // We don’t always have to , but lets tell the hardware we are using the relayPin as an OUTPUT
digitalWrite(relayPin, HIGH); // Lets turn off the relay at the start with a HIGH
// ie 5 volts, that’s how my particular relay works.
}
// Begin the main loop
void loop(){
// Just put the dit’s and dah’s in order here to say what you want to say in Morse add some // delays if you need them..
dah(); //we have made our own command really, look below and you’ll see it defined
dit(); // when we call it like a regular command, everything in the parentheses executes
dah();
dit();
delay(letter);
dah();
dah();
dit();
dah();
delay(2000); // lets wait afew seconds before going back to the start of the loop
}
// Below are functions, we can call them in the main loop to save us having to rewrite them each time
void dit(){
digitalWrite(relayPin, HIGH); // Make sure relay is OFF before we start
digitalWrite(relayPin,LOW); // A LOW or zero volts on the relayPin turns our relay ON
delay(dit_time); // wait the specified time
digitalWrite(relayPin, HIGH); // We better turn relay off now we are finished
delay(space); // wait before starting next part
}
void dah(){
digitalWrite(relayPin, HIGH); // Make sure relay is OFF before we start
digitalWrite(relayPin,LOW); // A LOW or zero volts on the relayPin turns our relay ON
delay(dah_time); // wait the specified time
digitalWrite(relayPin, HIGH); // We better turn relay off now we are finished
delay(space); // wait before starting next part
}