2ManyProjects Search
Home
Getti ng ng Started With Arduino
Woodwor ki king Pr oj ojects
Software Tutorials
SOS ( Arduino Due ) Add comments comments
This Arduino Due Tutorial is exactly the same as the SOS example done in the robot tutorial, except it is done with the Arduino Due. The Arduino Due is one of the newer boards, and at the time of this writing you need a newer version of the Arduino IDE. You need version 1.5.6-r2 BETA. If you already have an earlier version, you should not have to remove it, you can have two different versions installed at the same time. Almost all tutorials you ’ll find start by making an L ED (light emitting diod e) blink, and this is a great way to start since it takes the absolute minimum amount of parts and steps to make a functioning project. So we’ll do the same here. If you already have experience with Arduinos, then you can probably skip or just read through this section.
Electronics
Imagine that you are stranded at sea and can see other boats or planes, but don’t have a radio or cell phone? What to do, send an SOS of course, that is the international Morse code distress signal. ( SOS does not officially stand for anything, it is used because it is very easy to send/receive and remember, 3 dots, 3 dashes, 3 dots … . ( http://en.wikipedia.org /wiki/SOS ). Creating an SOS beacon with an Arduino is about as easy as it gets, a tiny LED like we’ll be using would not be bright enough to signal another ship, you would need a lot more power than what the Arduino Due can supply by itself to run a light bright enough for that. This sort of thins can be done by having the Arduino connected to an electrical relay, but that’s a different lesson altogether, we’ll stick with just a simple LED for now. We only need 3 pieces of hardware for this first tutorial 1) Arduino Due. 2) USB cable. 3) one LED, just about any LED should work. First, here is a quick overview everything you’ll need to do for this part of the tutorial. 1) Download and install the Arduino software. 2) Connect the Uno to your computer using the USB cable. 3) Open the IDE. 4) Select your board and Serial Port. 5) Copy and paste the code from this site.
6) Upload the program. 7) Install the LED. The first thing to do if you have not already done so is to download and install the Arduino software, it is available for Windows, MAC or Linux. I will walk you through how to install it for windows. It is just as easy to install on Mac or Linux, I’ll provide some links for those. For Installing Windows: You can find the installer at http://arduino.cc/en/main/software . You’ll notice that there are two files for windows, an installer, and a zip file. Its easiest just to use the installer as this will install the IDE and the drivers for you. If you instead download the zip file you will probably need to install the drivers manually. The official Arduino pages give a good description of how to do this. http://arduino.cc/en/Guide/windows. I always use the installer.
Then go to where you saved the Installer and run it.
For installing on MAC see: http://arduino.cc/en/Guide/MacOSX For installing on Linux see:
http://playground.arduino.cc/learning/linux Now that the Arduino software is installed, we need to connect the Due through the USB port.
Navigate to where you have installed your Arduino folder ( mine is C:\electronics\Arduino \arduino-1.0.5 ), and open the Arduino IDE. First we need to tell the Arduino IDE what board we are using and which serial port to use to program the board. Click Tools->Board->Uno,
TI's MSP430 16-Bit MCUs Ultra-Low Power RISC Mixed-Signal Microprocessors for Portable Apps.
then Click Tools->Serial Port-> COM1 ( or whatever serial port you are using ). It not always obvious which serial to choose, I usually keep trying until I find the right one.
Next, copy and paste this code into the IDE.
// flashes a LED in a 3dots-3dashes-3dots pattern const int led_pin = 13; void setup() { // set the digital pin as output: pinMode(led_pin, OUTPUT); Serial.begin(9600);
} void loop() { // write the 'S'. digitalWrite(led_pin, LOW); delay(250); Serial.println("writing low");
digitalWrite(led_pin, HIGH); delay(250); Serial.println("writing high");
digitalWrite(led_pin, LOW); delay(250); Serial.println("writing low");
digitalWrite(led_pin, HIGH); delay(250); Serial.println("writing high");
digitalWrite(led_pin, LOW); delay(250); Serial.println("writing low");
digitalWrite(led_pin, HIGH); delay(250); Serial.println("writing high");
// write the 'O' digitalWrite(led_pin, LOW); delay(1000); Serial.println("writing low");
digitalWrite(led_pin, HIGH); delay(1000); Serial.println("writing high");
digitalWrite(led_pin, LOW); delay(1000); Serial.println("writing low");
digitalWrite(led_pin, HIGH); delay(1000); Serial.println("writing high");
digitalWrite(led_pin, LOW); delay(1000); Serial.println("writing low");
digitalWrite(led_pin, HIGH); delay(1000); Serial.println("writing high");
}
Lets get the light blinking, then we’ll discuss how this code works. Click Sketch->compile and verify that the code compiles. Now click on the upload button, this will compile and upload your first Arduino sketch.
This is the most common place to get some errors, having the correct board and serial port selected is very important. Things will not work if they aren’t correct , and the errors you get might not be very informative. You also must have the drivers installed. Here is a list of the most common problems I see. I see this error whenever the board is not plugged in, or the wrong serial port is selected
and this is what I get when the wrong board is chosen
While I’m talking about errors, I’ll mention a few more common issues that I have encountered, not necessarily related to compiling and uploading. When using an Arduino that is being powered by batteries alone, if the batteries start to run down, you might expect that things just start to run slowly, but in reality all sorts of bizarre things can happen. If bizarre things start happening and there isn’t a good explanation as to why, it is always good to check your have a reliable power source. Sometimes having a a wire hooked up to pin 1 pr pin 2 can cause problems, because the serial monitor can also be used through ports 1 and 2.You can try disconnecting whatever is connected to ports 1 and 2, then upload your program, then reconnect whatever was connected to ports 1 and 2. Having an external power supply hooked up while you upload the program cause some problems, e.g. with the motor shield used in the robot tutorial, you’ll may need to disconnect power from the shield to program the Arduino, upload your program, then reconnect the power supply. Sometimes when doing a lot of tinkering and debugging, the Arduino just seems to get frazzled, and if I just unplug the usb cable and restart the IDE, then plug the cable back in, things go back to normal. Once we have successfully uploaded the sketch, we are ready to insert the LED, a LED is a polarized device, there is a (+) terminal and a (-) terminal, if you don’t hook it up correctly, it won’t work, and in some situations can catch fire and burn up, it can also burn up if it draws more power than it is rated for. This is why you need to pay attention to how much current
your LED can handle, if you hook a LED directly to a power supply it will probably catch fire and burn up. I went ahead and hooked up a LED directly to the Arduino Uno, because we are blinking the LED it does not draw current continuously. It would take a pretty small LED to get burned up here, but its possible, so if you aren’t sure you should hook up a current limiting resistor. If you are not familiar with the concept of a current limiting resistor, there are a bunch a great explanations on the web, for example https://www.sparkfun.com /tutorials/219. http://forum.arduino.cc/index.php/topic,3649.0.html. I’ve burned up LED’s, capacitors, and various other devices more times than I can count, its usually not a big deal, but be careful, usually you will start to smell it before you see it, if you do start to burn something up, just cut the power, ( you should always a have an easily accessible way to cut the power, e.g. plug the usb cable into the front of the computer so you can pull it out easily). Then don’t touch anything for a couple minutes to make sure things have cooled down. If you really do it well it will set off your homes smoke detector. You can also ruin your Arduino if you draw too much power from one of the ports, e.g. if you directly connect an output port to an input port with a piece wire. LED’s have some internal resistance, which prevents this from happening here. Here is a great write up of ways to damage your Arduino. ( http://ruggedcircuits.com/html/ancp01.html ) Alright, the easiest way to te ll positive from nega tive with LED’s is that one of the the wires is almost always longer than the other. The longer wire is positive (+), and the shorter wire is negative. The other way to tell is that there will be a flat side on the plastic casing of the negative wire. (if you really squint you can see the flat side of the LED in the second picture, it was really hard to get the camera to focus on it). Almost every LED will have one of these features, if not you are going to need a multimeter to figure it out. Use the ohm meter to measure resistance, you will see a smaller resistance when the polarity is correct (forwardbiased), and a larger resistance when its hooked up backwards ( reverse-biased). If you need more help with this check out http://www.allaboutcircuits.com/vol_3/chpt_3/2.html
hook up your led as shown here, the positive wire (the longer one) is inserted into port 13, and the negative wire ( shorter wire, flat side ) into the GND pin that is next to port 13.
And the LED should be flashing now, like this
Now that you have seen what it takes to produce a simple sketch and upload it to the Arduino, lets talk a bit ab out how the code wo rks. One thing I’m doing here that isn’t usuall y done with an introductory program is with all the Serial.println() commands, which are the simplest way to debug a sketch that is running on an Arduino. You simply include lines like Serial.println(“writing low”) , then with the usb cable still connected to the PC, click on Tools->Serial Monitor . In the window that pops up you need to select the baud rate that matches what you have it set to in the code, here it is set to Serial.begin(9600); when this code executes on the Arduino, the value will get written to the window.
I need to go on a little side rant here, my first exposure to programming was a very long time ago in a C programming class (this had nothing to do with Arduinos or embedded systems, just programmin g a computer) , and the instructor did not sh ow us or teach us how to us e a debugger. I had the same instructor for my second programming class, and again there was no instruction on using a debugger, I ended up getting shown what a debugger from a classmate about half way through that semester, and it cut down the amount of time I needed to complete assignments drastically. As the years went by and I continued to educate myself, the importance of good debugging grew more and more apparent. In particular, it was in one of my favorite books, the Art of Unix Programming, which despite its name is a book on general programming principles, and applicable to just about any sort of programming, that I first read the following quote by Brian Kernighan.
Controlling complexity is the essence of computer programming” [Kernighan-Plauger] . Debugging dominates development time, and getting a working system out the door is usually less a result of brilliant design than it is of managing not to trip over your own feet too many times. ( The art of Unix programming page 36)
The author lays out 17 rules or principles, which the book is written around, here is number 7.
7. Rule of Transparency: Design for visibility to make inspection and debugging easier.
and a quote later ( in Chapter 6)
Don’t let your debugging tools be mere afterthoughts or treat them as throwaways. They are your windows into the code; don’t just knock crude holes in the walls, finish and glaze them. If you plan to keep the code m aintained, you’re always going t o need to let light into it.
It is this last excerpt that I have come to appreciate the most, and what I’m trying to pass on to you, get yourself in the habit of thinking about your debugging from the very point you start a new program, not when the first problem crops up. This will not only make you more productive, but if someone else ever needs to read and/or modify your code, you will have done them an enormous favor, not just with debugging your code, but in telling them where to look, a enormous amount of information can be gleaned simply by knowing where the original author would have looked to debug problems. This is less relevant with smaller programs like these, but since debugging is what most programmers spend the most time on, I think you should take this approach with every program you write, be it for your smartphone, PC or Arduino, whether your program is small or large. Also debugging with an embedded system like Arduino is fundamentally harder to set up than with a general purpose computer like your PC since we are writing the code on a different device than it is executing on, there are many, much more sophisticated tools for debugging an embedded system, but that’s for another day. The Serial Monitor will be sufficient for this tutorial, and that’s what we will stick with. Having said all that, the debugging statements do clutter up the code a bit, so here is the code without them.
// flashes a LED in a 3dots-3dashes-3dots pattern const int led_pin = 13; void setup() {
// set the digital pin as output: pinMode(led_pin, OUTPUT); } void loop() { // write the 'S'. digitalWrite(led_pin, LOW); delay(250); digitalWrite(led_pin, HIGH); delay(250);; digitalWrite(led_pin, LOW); delay(250); digitalWrite(led_pin, HIGH); delay(250);; digitalWrite(led_pin, LOW); delay(250); digitalWrite(led_pin, HIGH); delay(250); // write the 'O' digitalWrite(led_pin, LOW); delay(1000); digitalWrite(led_pin, HIGH); delay(1000); digitalWrite(led_pin, LOW); delay(1000); digitalWrite(led_pin, HIGH); delay(1000); digitalWrite(led_pin, LOW); delay(1000); digitalWrite(led_pin, HIGH); delay(1000); }
Notice that there are 3 sections in this program, the setup() function, the loop() function, and everything outside these two functions. You will see at least these 3 sections in every Arduino progr am you write. The setup fu nction executes whe never you first power o n the Arduino, or press the reset button. Then th e loop function will e xecute indefinitely, over and over again. The line
const int led_pin = 13;
creates a variable that we will use to write to the port that the (+) wire of the LED is connected to. Then in the setup function the line
pinMode(led_pin, OUTPUT);
declares the port 13 is going to be used as an output, and that is all the setup that we will need, in the main loop we only need two commands
digitalWrite(led_pin, LOW);
which simply sets the output to be HIGH ( 5 Volts) or LOW (0 Volts), since the negative wire of the LED is connected to the ground port (0 volts), when we set port 13 to be HIGH , we’ll have a potential difference of 5V across the LED and it will light up. When we set port 13 to LOW , there will not be a potential difference and the LED will not illuminate. Lastly we just need to vary the amount of time between the LED being on and off. This is done with
C/C++/Ada Code Coverage Reduce timescales, risk & effort for DO-178B/C up to level A (MC/DC)
delay(1000);
where the time to delay is in milliseconds, 250 ms for the ‘S ‘ ( dots) and 1000 ms for the ‘ O ‘ ( dashes )
And that is it for your first Ardui no project! Congra tulations. Arduino Due Tutorial.
Intro->SOS->LCD Tutorial->Servo Tutorial->Timer Interrupts->Hacking Servos->Tone generator->DDS Tone Generator->Theremin 1->Theremin 2->RTTL Songs
Posted by danad at 3:53 pm
Leave a Reply
Name
(required)
E-mail
(required)
URI
eight ×
= 64
Your Comment
You may use these HTML tags and attributes:
title=""> SubmitComment
Notify me of follow-up comments by email. Notify me of new posts by email.
Search
Recent Posts Welcome!
Recent Comments danad on DDS Tone Generator danad on DIY panel saw
Ryan James on DIY panel saw backfromearth on DDS Tone Generator danad on DIY panel saw
Categories Uncategorized © 2014 My Blog Suffusion theme by Sayontan Sinha