
Arduino home page
The information given here assumes that you have an Arduino board with USB.
Contents
- Installation
- Arduino and AVR
- Serial communication from Arduino to computer
- Speaker beep example
- Switch example
- Blinkenlights example
- Further examples
Installation
Windows
Quite painless. The Howto at the Arduino site has more detailed installation instructions than the ones given here.- Download the IDE (Integrated Development Environment) from the Arduino site and unzip. Also unzip the 'FTDI USB Drivers' in the 'Drivers' folder.
- Connect the Arduino board and hardware wizard will start up. Point it to look for drivers in 'FTDI USB Drivers'. Open 'Control panel \ System \ Hardware' and check which COM-port the board has become.
- Open up the Arduino IDE and pick the correct COM-port under the 'Tools' menu.
- Testing - Copy BlinkingLED from the Arduino site. Press 'Verify' (the play button) to compile and 'Upload' to transfer the program to the Arduino. You may have to press the reset button on the Arduino board shortly before transferring the program.
Linux
Not painless :-(To run the Arduino IDE (Integrated Development Environment) you first need to install the AVR tool chain and, if you don't already have it, Java. The largest problem is getting avr-c++ (which is required by the Arduino IDE) installed. It's possible to get most of the AVR tool chain installed by following the avr installation instructions for atmega128.
2010-03-25: Installing avr-g++ is now quite painless. Just do
yum install avr-gcc-c++or
apt-get install gcc-avrdepending on which package manager is used on your distro. Provided that you already have java installed the ardunio installation is now just a matter of unpacking the arduino tarball.
Mac
I have not tried running the Arduino IDE (Integrated Development Environment) under Mac. Read the Howto at the Arduino site for Mac installation instructions.Arduino and AVR
It is possible to use AVR to program the Arduino instead of using the IDE. This section has grown a bit and now avr-c programming Arduino has its own page.Serial communication from Arduino to computer
Serial communication back to the computer is really helpful for example when debugging programs. Here's a little test program that sends "Hello world!" at a rate of once per second back to the computer.void setup() { //sets the baud rate and initializes serial communication Serial.begin(9600); } void loop() { //send message Serial.println("Hello world!"); //wait for one second delay(1000); }
Linux
You can use minicom to receive messages. Run minicom (you probably need to be root) and press Ctrl+A Z to display the available commands. Press 'o' for Options and choose 'Serial port setup' and make sure you've got the correct port selected (most likely /dev/ttyUSB0). Also make sure that the baud rate is set to 9600 8N1 and that 'Hardware flow control' is set to 'no'. Once you are done with the settings use 'Save setup as dfl' and minicom will start up with the correct settings next time. Quit minicom and restart it to activate the new settings. You should now start receiving the 'Hello World!' messages.Windows
In Windows XP and older you use hyperterminal.. If you have hyperterminal it is located in \ Program \ Accessories \ Communication. Run the program and pick a name for the connection. Pick the correct COM-port in 'Connect with'. Set bits per second to 9600, databits to 8, parity to none, stopbits to 1, and flow control to none. When you press connect you should start receiving the "Hello world!" messages.Hyperterminal was apparently dropped from windows in Vista, and is not available in Windows 7 either. If you do not have hyperterminal I suggest that you use PuTTY instead.
3.3 Arduino
It turns out that the Arduino IDE also can show you serial messages. Press
The hyperterminal (windows) and minicom (linux) setup instructions may still be useful, though.
Speaker beep example
Connect a small speaker (I used a leftover speaker from an old PC case) to GND and pin 13. The example program below will beep at 220Hz for one second, and then stay silent for one second.void setup() { } /* square wave tone on pin# pin freq in Hz and duration in ms */ void play_tone(int pin, int freq, int duration) { int i = 0; int sleep = 1000 / (2*freq); int periods = duration / (2 * sleep); pinMode(pin, OUTPUT); for (i = 0 ; i < periods; i++) { digitalWrite(pin, 1); delay(sleep); digitalWrite(pin, 0); delay(sleep); } } void loop() { play_tone(13, 220, 1000); delay(1000); }In this example pin 13 is used. Pin 13 has a built in resistor and this makes the beep less loud than it would be otherwise. Believe me - It will get annoying quickly enough, even at the lower volume. Pretty much the same effect can also be acomplished with the built in tone and noTone commands.
Switch example
Connect a switch between GND and pin 7, and upload the program below./* Connect a switch between GND and pin 7. Will transmit "Closed" / "Open" whenever the switch closes or opens. */ int switch_state = HIGH; void setup() { Serial.begin(9600); pinMode(7, INPUT); // activate pull-up resistor on pin 7 digitalWrite(7, HIGH); } void loop() { int state = digitalRead(7); if (state != switch_state) { switch_state = state; if (switch_state == HIGH) Serial.println("Open"); else Serial.println("Closed"); } delay(1); }The most interesting thing going on in this example is the activation of pin 7's pull-up resistor. Without it, opening the circuit would cause pin 7 to float freely, and not be in a well defined state.
Use the IDE's Serial Monitor to view the data sent from the Arduino.
Blinkenlights example
Here is an arduino program that listens for serial commands to tell it to turn LEDs on or off. Apart from pins 0 and 1, which are used for rx and tx, any digital pin can be used to control a LED. Pin 13 has a built in resistor, which will limit the current through the LED and keep it from breaking. If you want to use the other pins too, you will need to add resitors (let's say 1k Ohm, same as pin 13's built in resistor) to protect your other LEDs./* Blinkenlights program, which listens for commands to turn LEDs on or off. Serial data : A - turn on LED 0 (at pin 2) B - turn on LED 1 (at pin 3) ... a - turn off LED 0 b - turn off LED 1 ... */ // number of LEDs int n_leds = 12; // skip pins 0 and 1. They are needed for serial rx and tx int first_led = 2; // on / off commands int first_led_on = 'A'; int first_led_off = 'a'; // serial data int data; void setup() { int i; for (i = 0; i < n_leds; i++) { pinMode(first_led + i, OUTPUT); digitalWrite(first_led + i, LOW); // start with everything off } Serial.begin(9600); } void loop() { //look for incoming data if (Serial.available() > 0) { data = Serial.read(); if (data >= first_led_on && data < first_led_on + n_leds) { //turn on digitalWrite(data - first_led_on + first_led, HIGH); } if (data >= first_led_off && data < first_led_off + n_leds) { //turn off digitalWrite(data - first_led_off + first_led, LOW); } } delay(1); }With the program loaded you can now turn a LED connected to pin 13 on by using the serial monitor to transmit an 'L', and off with 'l'.
One application of this is to monitor activity in certain files, for example log files. Below is a python program that monitors files, and sends commands to the arduino blinkenlights program. The pyserial package is required by this python program.
#!/usr/bin/python import sys import serial import time import os device = "/dev/ttyUSB0" if (len(sys.argv) > 1) : device = sys.argv[1] ser = serial.Serial(device) ser.baudrate = 9600 blink_data = [] # Add more entries to taste # Format: (file to monitor, On command, Off command) blink_data.append(("/var/log/httpd/access_log", 'L', 'l')) mod_time = [] for entry in blink_data : mod_time.append(time.localtime(os.path.getmtime(entry[0]))) while (True): index = 0 for entry in blink_data : t = time.localtime(os.path.getmtime(entry[0])) if (mod_time[index]) != t : mod_time[index] = t ser.write(entry[1]) # Send ON else : ser.write(entry[2]) # Send OFF index = index + 1 time.sleep(1)If you are using Linux it is quite convenient to use udev to symlink to your Arduino instead of assuming that it turns up as /dev/ttyUSB0.