Showing posts with label Arduino Hacks. Show all posts
Showing posts with label Arduino Hacks. Show all posts

Sunday, March 8, 2009

Windows XP Build Script for Arduino

If you use an Arduino board with Windows XP...

If you like to code in C/C++ and have total control of compilation...

If you dislike GUI IDEs and prefer coding in <your favorite text editor> with a makefile...

A Windows XP Build Script is all you need!!!


Arduino (Show/Hide)

If you want to read more about Arduino, unhide this section.


The Arduino IDE and build process

The Arduino IDE seems to be tailored to smaller programs in which all codes can fit into one file. In my case, I'd like to have multiple files to keep things organized. I'd also want to have codes shared by two or more projects, so I have files in separate folders. The IDE seems to get confused by the file paths and it was not long until I found it too bothersome to get the projects to compile and link properly. I love the legendary makefile and command line tools after all.

The entire Arduino platform is actually a frontend of the WinAVR tools, which is essentially an ATmega128 extension of the GNU collection. So all I needed was to write a batch file that executes the proper commands to compile, link and download the program. Of course, this could be a difficult task, but it turned out that I wasn't the first one, as I found exactly what I wanted at the Arduino Playground.

The Windows command line build was written by Don Cross and is a build script that works seamlessly. Though, this tool comes in three batch file and was a little bit difficult to digest/customize at the first glance. Also, the tool assumes that all your code comes in one single file. These batch files weren't exactly what I wanted, but they did provide a good starting point!

THE Arduino build script

After some poking around, I wrote up a makefile-like build script. The script, along with an example project, can be downloaded here:

ArduinoBuildScript.zip

Below is a description of the build.bat build script:

1 Configs (Show/Hide)

2 Parse command line option (Show/Hide)

3 Prepare environment variables (Show/Hide)

4 Clean output files (Show/Hide)

5 Compile source codes (Show/Hide)

6 Link to elf file (Show/Hide)

7 Download to microcontroller (Show/Hide)


The Example project and Further customization

To run the example, simply hook up your Arduino board, edit the COM port in the script and execute buildExample.bat. The example demonstrates compiling and loading a multi-file project. Note that I made it include a 3rd party library called SoftwareSerial. The project doesn't really need it but I included it to demonstrate how you can simply provide a space delimited list to tell the build script to compile and link the 3rd party libraries.

NOTE: It is important that you execute the init() function at the beginning of your main() function. The init() function initializes the hardware of the board etc. Also, remember to include the WProgram.h header file whenever you use an Arduino function call (like Serial and digitalWrite).

This build script hopefully provides a good basis of a customized makefile for everyone. My original intention was to also code other features into the script, but I decided that there are too many possible features you can add and it's down to your choice of functionality. Without modifying any codes this script supports compiling and loading multiple source files in multiple folders.

With a little modification, you can also customize this script to do the following:

  • support more than 1 config with the same script (like a DEBUG preprocessor flag): You can add a command line option, then use an if statement to set the variables to respective values.
  • support more than 1 project: Similar to above, but use a command line option and an if statement to switch between sets of source files and output name. The project outputs will locate in its own folder so you can compile multiple projects without affecting each other.
  • save the build process console output to a file: change the BUILDLOG variable

... and lots of other features! Once you get familiar with the script, you should be able to tweak it according to your own need. So enjoy!

Thursday, February 19, 2009

I2C EEPROM for the Arduino

The Arduino is a nifty little board as most of you probably know already. I was doing a school project on it and found that I needed some external storage. Now, there are a lot of storage options available. SD cards and EEPROM on the SPI bus seem to be a popular choice, but I couldn't sacrifice 4 I/O pins just for storage.

I found code and tutorial for using I2C EEPROMs on the Arduino which was perfect since it would only take up 2 pins (pin 4 and 5) and I could put up to 8 EEPROMs on a single bus. The problem was that the tutorial was in Portuguese, (and I found another one in Spanish) so I ended up just fumbling through it myself. For the rest of you, here's the English guide.

The EEPROM Chip that i used was AT24C256B which I bought from Digi-key for $1.43, but any 24XX series EEPROM should be the same. Sparkfun carries the 24LC256 for $1.95 if you don't want to pay for Digi-key's ridiculous shipping charges (after shipping, Sparkfun will be much cheaper). The ones I listed above are in DIP-8 package and provide you with 256Kbits of storage (32KBytes) but you can purchase these in different packages and capacities. Since the 24LC is cheaper, the rest of the tutorial will assume using 24LC.

Wiring it together is super easy. To save everyone the trouble, I'll lay it out simple and straight forward:

Pin 1 2 and 3 determine the address of the chip; I2C uses 7 bit addresses + 1 control bit. With the code available on the Arduino Playground link above, you don't have to worry about the control bit. The EEPROM chip's address in binary will be 1010XXX where XXX is Address 2, Address 1, and Address 0 in that order. For example, if Pin 1 2 3 are Low Low High in that order, the binary address will be 1010001, or Hex 0x50.

For simplicity, I connected them all to Ground, thus giving me address of 1010000, or hex 0x50. It is important to remember to pull SDA and SDL up to 5V using a 1K resistor since those pins aren't pulled up internally at either end. Failing to pull them up will result in garbage when communicating. The picture below shows how I wired up my EEPROM:



The last part is actually using the code in your software. Since the creator for the functions linked above hasn't created a .h file for his stuff, you will simply have to copy and paste the entire snippit to the beginning of your code. Click here to get the code from Arduino Playground.

The code includes four functions i2c_eeprom_write_byte, i2c_eeprom_write_page, i2c_eeprom_read_byte, and i2c_eeprom_read_buffer. They are self explanatory if you read the code; for example, to write the data byte "0xAB" to the 1st block (remember a 256kbit EEPROM has 32708 blocks available) of the EEPROM whose address is 0x50, you would use:

i2c_eeprom_write_byte( 0x50, 1, 0xAB );

And to read the data you would write:

temp = i2c_eeprom_read_byte( 0x50, 1 );

That's it for this simple tutorial, those who want more information can read the EEPROM's datasheet and the details of the code provided.

Update: I found another guy who has a tutorial (Although it is in Spanish) of this up in his blog. He even has a picture of it all wired up on the breadboard. Click the picture below to jump to his tutorial