Programming Arduino

From ShawnReevesWiki
Jump to navigationJump to search

About the Arduino

Arduino is a programming system using Atmel's AVR micro-controllers and standardized, open-source hardware and software. All the software is free, for Mac, Linux, and Windows. The basic hardware is one of many boards which contain the micro-controller, a power management system, optional USB or other communications hardware, and interface ports. Shields are extra hardware, such as sensors, network hardware, or displays, made to connect easily to the ports of certain boards.

Sketches

Sketches are programs written in the Arduino integrated development environment (IDE). The IDE includes an interpreter that compiles C-based code into the language used by the micro-controller, and on request sends that interpretation to the target micro-controller via a programmer.

Sketches can be run with or without the Arduino bootloader pre-loaded onto any compatible micro-controller. With a bootloader, programs can be sent to the TX and RX pins of the micro-controller, which are rarely used for anything but serial communication with a computer or other micro-controller. Without a bootloader, programs must be sent to the three SPI pins, which are often used for other purposes. Thus, using the bootloader is the preferred method.

Loading the Arduino bootloader

It is possible to use the AVRISP program for Arduino to act as a programmer, in order to load the bootloader program onto an Atmel AVR micro-controller. An intermediary Arduino takes serial communications from a computer and sends to the target micro-controller the necessary signals via SPI pins and a reset pin.[1]

Uploading sketches

To a USB-capable Arduino

Arduino boards with built-in USB-to-TTL serial converters are easy to program. One just selects the corresponding serial port in the "Tools>>Serial Port" menu item, preferring "tty." over "cu." in Mac/*nix systems.

To a USB-less Arduino

Arduino boards without USB, such as the Mini Arduino Pro, include pins to attach to ground, TX, RX and DTR pins of a serial port or USB-to-TTL adapter. USB-to-TTL adapters may also supply the working voltage.

To a breadboard Arduino via UART

Breadboard Arduinos, in other words, Arduinos made by hand around an AVR micro-controller with a bootloader, may be programmed by connecting their TX, RX, and reset pins to the RX, TX, and DTR pins of a serial port.[2]

One can also use an Arduino as a USB-to-TTL adapter if the AVR micro-controller is removable, as in the Arduino Uno.[2]

To a breadboard Arduino via ICSP

One can skip using the bootloader, freeing the space that would be used by the bootloader, uploading a sketch through the SPI interface, using an intermediary programmer like the AVR-ISP or the Arduino-as-ISP.[3][1] A problem with this method is that it uses the SPI ports, which you might be already using for your circuit. To send a program to a programmer this way, go to the menu item File>>Upload Using Programmer instead of File>>Upload.

If you upload a program this way, you might neglect burning the fuses necessary for your project. For example, the AVR probably comes from the factory set to use an internal clock instead of a crystal. To set the fuses to match Arduino, upload the bootloader before uploading the program. The boot loader will be overridden, but the fuses will be set properly for Arduino.

See also

Programming AVR micro-controllers discusses programming the same micro-controllers as used by Arduino, but not with the Arduino IDE.

Examples

My frequency meter uses an Arduino's built-in floating point math functions and its microsecond clock, accurate thanks to a 16MHz crystal.

References