0-2u.com

Arduino Tutorials & Code

Arduino Bootloader with STK500

I followed this tutorial to burn the Arduino bootloader onto an AVR ATMEGA8. The one thing that I didn't like was going through all of the AVR Studio menus, clicking checkboxes, etc. Instead, I just wanted a batch file that would do everything all at once.

So here it is:

1) Get the bootloader hex file onto your desktop or in a folder or something.
2) Create a new batch file in the same directory and open it to edit.
3) Type this:

: Set fuse bits, lock bits, voltages
D:\\STK500\\Stk500.exe -dATmega8 -ut5.2 -ua5.0 -!3686400 -lFF -L -fCADF -F
: Burn Arduino hex bootloader file
D:\\STK500\\Stk500.exe -dATmega8 -pf -e -vf -ifbootloader.hex
: Lock it down
D:\\STK500\\Stk500.exe -dATmega8 -lCF -L

4) Change the code for your setup. First, where is your stk500.exe? It might not be on a D drive! Second, what is the hex file called? I changed the name of the file to be "bootloader.hex"
5) Attach your STK500 and plop in a new ATMEGA8, set it up like in the tutorial above...
6) And double click your batch file to run it.

If you want to learn more about the command-line options, look at section 5.4 and 5.5 of this PDF. I just implemented the steps from the tutorial above into the command-line language outlined in the PDF documentation.

RF Link between two Arduinos

This tutorial shows how one Arduino can send data wirelessly to another Arduino. This can be accomplished in a number of ways, but I am showing how do to it using these 315MHz RF Link modules. When I bought mine it cost $11.95, but the price has gone up to $13.95. There are probably much better choices at the $20 range - these RF Link modules can only broadcast from transmitter to receiver, as opposed to creating a network of inter-communicating microcontroller. But if you're fine with one-way communication across an open space, then the RF Link is fairly easy to use - you just pretend it's a physical serial connection. (if a "serial connection" doesn't sound easy to you, then have a look at this tutorial).

The RF Link comes in two parts, a small transmitter and a wide receiver. The drawing below shows how you'd wire each of these up to any microcontroller (each of which is represented by a brain). They are quite easy to wire up as you can see in the "walkthrough tutorial" provided by Sparkfun. I found that the antenna wasn't really optional beyond a distance of about 24 inches with the transmitter getting 5V. There's no real explanation of this, but I cut a wire 23 cm long to use an antenna and then I connected it to ground through a 1uF capacitor (my first guess was 0.1uF but all the data was garbled until I switched, so be flexible with this value!)

If you're using the Arduino platform, then here is some basic code for the following situation: I push a button and a PC across the room is made aware of this. In other words, a button is read by Arduino #1, which pushes information through the transmitter. Somewhere else in RF range, Arduino #2 is pulling in data from the receiver and sending some of this data up the USB cord to a computer.

The following is the transmit code for Arduino #1. Notice that I used the software serial library so that you can save the Serial port for something else later. Probably the most important twist in this code is that the trasmitter needs to be sending data CONSTANTLY or else the receiver will be left hanging and will start reading random junk floating in RF space -- by sending data all the time, just imagine that you're overpowering all the junk. So you'll probably need to decide on a byte of information that actually means "nothing" or "no data now" or "off". I chose 98, which is wonderfully arbitrary. Finally I'm assuming you've got a normally open switch wired to pin 12 (pulled down by a 10K resistor).

#include

/*
Read a pushbutton and send a value
to another microcontroller.
*/

#define rxPin 2
#define txPin 3
#define buttonPin 12
#define ledPin 13

// set up a new serial connection for communicating with RF transmitter
// (this also frees up Arduino's built in serial port for PC communication)
SoftwareSerial rfSerial = SoftwareSerial(rxPin, txPin);

byte val = 0;
byte onState = 99;
byte offState = 98;

void setup() {
pinMode(buttonPin, INPUT); // declare pushbutton as input
pinMode(ledPin, OUTPUT); // declare led as output
rfSerial.begin(2400); // our RF Link is a 2400 baud model (make sure you check this!)
// start by blinking high and then low so we can tell the setup routine has been completed
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
}

void loop(){
val = digitalRead(buttonPin); if ( val == HIGH ) { // check if the input is HIGH (button pressed)
rfSerial.print(onState, BYTE); // OK send it to the RF transmitter
} else {
rfSerial.print(offState, BYTE); // send a 0 through the transmitter
}
}

OK and here is the receiver code which goes on Arduino #2. Once again I'm using the SoftwareSerial library, but this time it's necessary because I need to use the usual Serial port to send data up the USB to the computer. The loop is filled with a little extra code to avoid sending the same byte over and over and over. A byte is only sent if it is new (changes from "off" byte to "on" byte for instance and no data would be sent again until it goes back "off").

#include

/*
Read from a RF Link receiver module
and get the data into a computer.
*/

#define rxPin 2
#define txPin 3
#define ledPin 13

// set up a new serial connection for communicating with RF receiver
// (this also frees up Arduino's built in serial port for PC communication)
SoftwareSerial rfSerial = SoftwareSerial(rxPin, txPin);

char prevChar = 0;

void setup() {
// set up the input and output pins
pinMode(rxPin, INPUT); // set up pins for serial comm. with RF receiver
pinMode(ledPin, OUTPUT);
// initialize serial comm
rfSerial.begin(2400); // begin serial connection with RF Link unit
Serial.begin(2400); // begin serial communication over USB to the computer
// blink LED on and then off just to let us know the setup routine is complete
digitalWrite(ledPin,HIGH); // turn on LED
delay(1000);
digitalWrite(ledPin,LOW); // turn off LED
}

void loop(){
char someChar = '0';
someChar = rfSerial.read(); // read whatever the RF Link has to offer
// print out the character:
if (someChar!=prevChar) { // only print out new data (don't print 0 a billion times in a row)
Serial.print(someChar, BYTE); // send data to computer
prevChar=someChar; // store what we just read
}
}

Servo Motor Control with Arduino

*Great writeup on using a servo with Arduino and it has pictures and schemas!

The problem with both of the link above is that it assumes you have a potentiometer attached, and the servo timing is based on that. The first step would be a new function that strictly turns a servo:

int minPulse = 500; // Pulse for minimum servo position (microseconds)
int maxPulse = 2500; // Pulse for maximum servo position (microseconds)
int refreshTime = 20; // the time needed in between pulses (milliseconds)
int servoPin = 2; // you decide which pin to put the servo on!

int nowPulse = 500; // nowPulse will always store the current pulse that we're giving to the servo
int temp; // always good to have a temp variable

// this function stands on its own... all you have to do is make sure the "nowPulse" variable is updated and that it equals something between "minPulse" and "maxPulse"
function doServo() {
// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(nowPulse); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
lastPulse = millis(); // save the time of the last pulse
}
}

// replace this with whatever you want... read an analog sensor, arbitrarily choose a number, do something based on time... whatever. I'm choosing to listen to the computer for an instruction.
void calculateNowPulse() {
while (Serial.available()>0) { // while there are bytes to read
temp = Serial.read(); // read one
nowPulse = minPulse + 15*temp/2; // this seems arbitary but its actually just approximately precalculating:
nowPulse=minPulse+temp*(maxPulse-minPulse)/255
}
}
void setup() {
pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
pulse = nowPulse; // Initialize the motor position value
Serial.begin(9600); // let's get this PC connection party started
}
// the loop is super simple... figure out how long the pulse should be and then doing servo stuff
void loop() {
calculateNowPulse();
doServo();
}