Find all your DIY electronics in the MakerShed. 3D Printing, Kits, Arduino, Raspberry Pi, Books & more!

Make: Projects

Arduino in C: See Your Microcontroller in a New Way

In this project, I will teach you how to program an Arduino in pure C. The Arduino language is simply just a small refinement of C making it easier to code. If you've already programmed in C or on the Arduino, that will help you greatly.

Arduino in C: See Your Microcontroller in a New Way

For this project, you need to have a Linux computer, or else the things I describe will not work. Don’t fret, though, because there are plenty of tutorials out there that will help you. Piecing this together from multiple sources has been hard, but I’ve been able to do it on my Arduino Duemilanove and Arduino clone. Finally, I would like to say thanks to the following sources, because they helped me the most and may help you. All of my code and setup was based on these tutorials! Thank you!

http://www.javiervalcarce.eu/wiki/Progra…

http://iamsuhasm.wordpress.com/tutsproj/…

https://www.mainframe.cx/~ckuethe/avr-c-…

http://www.micahcarrick.com/avr-tutorial…

Related

Steps

Step #1:

PrevNext
Arduino in C: See Your Microcontroller in a New Way
  • 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:

PrevNext
Arduino in C: See Your Microcontroller in a New Way

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:

PrevNext
Arduino in C: See Your Microcontroller in a New WayArduino in C: See Your Microcontroller in a New WayArduino in C: See Your Microcontroller in a New Way
  • 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:

PrevNext
Arduino in C: See Your Microcontroller in a New Way
  • 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

Step #5:

PrevNext
  • 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!

blog comments powered by Disqus

Related Supplies at Maker Shed

Follow

Get every new post delivered to your Inbox.

Join 26,225 other followers