Step #1: Next
- After you have all of your parts (note that the USB cable can be replaced with another programmer, as long as you can program your Arduino or Arduino clone), you can begin installing avr-gcc (the C compiler for the Arduino). On Ubuntu, in a terminal window type:
- sudo apt-get install gcc-avr
- Enter your root password, and that should do it.
- For other distrubutions of Linux, install the gcc-avr package in the same way that you would another package.
Step #2: Next
- Once that's done, you can begin. Create a new folder on your desktop called "AVR-GCC_FORMAT" or something like that. Open it and read all of the instructions. Once done with that, you can move on.
Step #3: Next
- Now that you've done that, let's talk about what just happened. The Makefile is what compiles the "file.c" file and dispenses a .hex file, along with a bunch of other stuff. To set the makefile to your Arduino, you might have to tweak it a bit. If your Arduino is running on an ATmega328P, you're fine and can move on. If you are running any other chip (such as an ATTiny or another version of the ATmega), you will have to change the MCU_TARGET variable in the Makefile. Simply go into it, and place a # before the line:
- MCU_TARGET = atmega328p
- The # denotes a comment, so you're "commenting out" that line of code. Then, find the chip that's yours (say it's an ATmega168) and remove the # before that line. Save the file, and you're done.
- Now, you can rename file.c if you want to. This is quite a dull name, after all. I'd recommend flashlight.c, but anything works if it ends in ".c" (and follows a bunch of weird restrictions that you must be aware of). I'd strongly recommend just to go with flashlight.c.
- After changing that, you have to change the Makefile's first two lines to:
- PRG = flashlight
- OBJ = flashlight.o
- so that the Makefile knows what it's compiling.
Step #4: Next
- Now let's get to compiling and downloading!
- First, make a copy on the desktop of your AVR-GCC_FORMAT folder. Then, open a terminal window and cd to that folder. Type the command make and the makefile will run. A bunch of files will appear. This is why I recommended that you do this inside a folder rather than on your desktop or in your home folder.
- Now, once you're done with that, plug in your Arduino and type the following in the terminal window (make sure to reset your Arduino just before entering the command):
- avrdude -p version -P location -c stk500v1 -u -U -F flash:w:filename.hex
- Make sure you replace version with the name of your chip (m328p for ATmega328P, m168 for ATmega168, or type in something random to cause an error and you'll get the rest of the list).
- location should be replaced with something like /dev/ttyUSB0 if that's the location of your Arduino (if you're not sure, and it's plugged into a USB port, try that first).
- Finally, filename.hex should be replaced with the filename, which in this case is flashlight.hex.
- For example, say you were downloading flashlight.hex onto an ATMega328P at /dev/ttyUSB0. You'd type: avrdude -p m328p -P /dev/ttyUSB0 -c stk500v1 -u -U -F flash:w:flashlight.hex
- All the programming is done!
- Simply connect an LED from pin 13 to GND, connect a pull-down resistor (10k) from pin 4 to GND, and connect a wire to GND. Touch the other end of the wire to the part of the resistor closest to pin 4, and the light will turn on. Do it again, and it will turn off. You have completed your first project programming an Arduino in pure C!