Arduino Tips Tricks and Techniques - 2 PDF
Arduino Tips Tricks and Techniques - 2 PDF
Guide Contents 2
Arduino UNO FAQ 4
Arduino Timeline 4
New USB Chip 5
Why not just go with a atmega32u4? 6
How can I change the USB firmware? 6
Does the Uno use a resonator or a crystal for the processor clock? 7
So the Arduino is not as precise, timing-wise? 7
Why not have one 16Mhz crystal shared between both? 8
OK well lets say I don't care about that... 8
Whats with the FCC logo on the back? 8
A new Bootloader? 8
Why not just use the '8u2 as a programmer? 8
How does the new '8u2 affect Arduino-derivatives? 9
I tried to find a place to buy some '8u2s and couldnt locate any! 9
So does this mean there may be an Arduino shortage? 9
Did the Arduino team move from the FTDI chip to the '8u2 to screw over derivative-makers? 9
Are Shields still going to work? 9
Will enclosures, plates, etc still work? 10
More 3.3v power! 10
Why is the Arduino chip running at 16MHz when it can run at 20MHz? 10
Is it still Open source hardware and software? 10
UNO R2 and R3 10
Arduino Libraries 12
Need Help Installing a Library? 12
What is a library? 12
Using Libraries 12
What's in a library? 13
It's important to remember! 14
How to install libraries 14
Bootloader 16
Bootloader for the Atmega328 16
"No-Wait" Bootloader 16
No-Hang Bootloader 17
Upload Sketches with AVRDUDE 18
Upgrade 20
Introduction 20
Replace the Chip 20
Download an Arduino IDE with ATmega328 compatibility 21
3.3V Conversion 22
Introduction 22
Replace the Regulator 22
Replacing the Fuse 25
Arduino Hacks 28
Bumpers 28
Free up some RAM 28
ArduinoISP 29
NB this is just our opinion and interpretation of some of the decisions made by Arduino. We aren't associated with
Arduino, and don't speak for them! If you have to get an Official Response to your Arduino question please contact
them directly. Thx!
NB2 Still in progress, we're collecting common questions to answer. If you have more questions, please post them in
our forums.
Arduino Timeline
But first…some history! First there was the serial Arduino (what's the name of it?) with RS232 which was not used
outside of the Arduino team & friends.
The first popularly manufactured Arduino was called the NG (New Generation, like Star Trek, yknow?) The NG used the
Atmega8 chip running at 16 MHz and an FT232 chip for the USB interface. The bootloader takes up 2KB of space and
runs at 19200 baud.
The next version was the Diecimila. The Diecimila updated the chip from the Atmega8 to the Atmega168. The great
thing here is double the space and memory (16K instead of 8K). It still ran at 16MHz. The Diecimila also added two extra
header pins for 3.3V (from the FTDI chip) and the reset pin which can be handy when a shield is covering up the Reset
button. The bootloader takes up 2KB of space and runs at 19200 baud. Auto-resetting was also added which makes
life awesomer for everyone.
In 2009, the Duemilanove was released. This one also upgraded the chip again, to the Atmega328. Yet another
In 2010, we have the Uno! The Uno still uses the 328P chip and the power switcher. It has a smaller bootloader called
OptiBoot (more space for users' projects) that runs at 115K. So even though the chip is the same, you get another 1.5K
of extra flash space that was previously used by the bootloader. The FTDI chip has also been replaced with a
atmega8u2 which allows for different kinds of USB interfaces. Finally, there's an extra 3.3V regulator (LP2985) for a
better 3.3V supply. whew!
The good news about the FT232RL has royalty-free drivers and pretty much just works. The bad news is that it can -
only- act as a USB/Serial port. It can't act like a keyboard, mouse, disk drive, MIDI device, etc.
The Uno has changed that by exchanging the FT232RL chip with an atmega8u2 chip. There are a few things that are
possible with this new chip but before we discuss that lets make it clear that by default, this chip acts identically to the
FTDI chip that it replaces. It's just a USB-serial port!
One improvement in updating the chip is that, previously, Mac users needed to install FTDI drivers. The 8u2 imitates a
'generic' CDC serial device. So now, Mac users do not have to install a driver. Windows users still need to install the
.INF file but luckily there are no drivers. This means there will be fewer problems with new versions of windows. There
is no way to have a serial USB device that doesn't require an INF file in windows, sadly :(
The big thing that is nice about the 8u2 is that advanced users can turn it into a different kind of USB device. For
example it can act like a keyboard or mouse. Or a disk driver. Or a MIDI interface, etc. Right now there are no examples
of how to do this, but we hope to post some shortly.
And, finally, going with the 8u2 reduced the price of the board which made up for some of the other extras.
Right now there are a few Arduino's with a 32u4 chip such as the Leonardo, Micro and Esplora
The code for the 8u2 is based on LUFA, Dean Cameran's totally awesome USB-AVR library that has great examples
and documentation. Its also completely open source.
On the other hand, the Atmega328p chip that is the core processor in the Arduino now has a 16mhz ceramic
resonator. Ceramic resonators are slightly less precise than crystals but we have been assured that this one was
specified and works quite well.
A new Bootloader?
There's a new bootloader. It works just like the old one - being an STK500-protocol compatible but its a quarter of
the size! Down from 2K, the new bootloader is a tiny 512b. This gives you more space for your project code! Yay! It's
also faster - 115K instead of 57.6k so you'll be uploading code in 3 seconds.
The Bad News is that you must make sure to select Uno in the Boards menu!!! If you don't things will be confusing
because the bootloader speed is wrong, and you won't get that extra 1.5K!
Overall, its a good direction, and the chips can be used in older Arduinos just fine (so you can upgrade your
Diecimila or Duemilanove to the Uno by simply replacing the chip).
For more detailed information about the bootloader, such as source code, please visit the Optiboot project page.
If you want to make your own Arduino-compatible board, you have a few choices:
1. Don't use an 8u2, go with an FTDI chip instead that comes with a VID
2. If you're planning to make more than one board for your personal use, you will have to purchase a VID from
USB IF for a one time $2000 fee
3. If you're making a single board for your own experimentation, you can pick a VID/PID that doesn't interfere
with any devices on your computer and substitute those in
4. You can purchase licenses for single VID/PID pairs from companies that develop USB devices (we dont have
any specific links at the moment)
However, you can't use the Arduino VID when distributing your own Arduino-compatibles! If the cost of a VID is too
much for you, simply go with an FTDI chip, K?
I tried to find a place to buy some '8u2s and couldnt locate any!
Yep, there is a worldwide shortage of Atmel parts right now. Even the chip used in the Arduino core (Atmega328P)
is really hard to get. This happens after recesssions. We hope that these and other Atmel chips will show up again in
places like digikey soon. Till then, keep searching on findchips.com!
Did the Arduino team move from the FTDI chip to the '8u2 to screw over derivative-makers?
While the appearance of a hard-to-get chip coupled with the VID/PID mishegas may seem to be a little annoying, we
don't think that means that the Arduino team is being malicious or attempting to make life difficult for people who
make derivatives. The move to an '8u2 makes the Arduino more powerful, and easy to use as there are fewer
drivers to install. While there is a shortage now, there will eventually be plenty of chips on the market.
Some people in the Arduino forum have thought of forming a group that would purchase a VID for Arduinites to use
in personal projects. This is a pretty good idea and its probably the best way to avoid VID/PID conflicts. Between
65,536 projects, that comes to under a nickel per PID.
And of course, because they didn't get rid of the bootloader system, you can always just use an FTDI chip.
One sad thing about older boards is that they had a 3.3v power supply but it was really just whatever the FTDI chip's
internal 3.3v regulator could give. You -could- get 50mA out of it, maybe. But high power stuff like XBees, SD cards,
some fast ADC or DACs could easily drag down the FTDI chip and reset the USB connection. The Uno solves this
problem by adding a new 3.3V regulator the LP2985 which can easily provide 150mA.
The LP2985 is a very high quality regulator, and will work great for powering stuff and as a nice solid 1% analog
reference.
Why is the Arduino chip running at 16MHz when it can run at 20MHz?
This is a common question. The reason is that the first Arduino used the Atmega8 which could not run faster than
16Mhz. As the chip has been upgraded they wanted to make the boards speed compatible. Arduino is also not really
intended for fast-processing (its only 8-bit anyways) so the chips are running at 16MHz.
UNO R2 and R3
During fall of 2011, the Arduino team revealed that there will be a new minor revision of the classic Arduino, the "UNO
R3" (revision 3). A lot of people have asked us about the R3 so here is everything we know so far.
1. The UNO R3 is not available to resellers until December 1st or so. Really! Nobody has them until then!
2. The UNO R3 is backwards compatible with the UNO - same driver, same uploading, same look
What is a library?
Libraries are great places, and not yet illegal in the United States! If you ever need to learn how to do something, like
say fix a motorcycle, you can go to your local library and take out a book. Sure you could buy the book but the library is
nice because as a resource you can get the book whenever you need it, keeping your house uncluttered.
Software Libraries are very similar. We already studied what a procedure is, in lesson 3: a procedure is a list of things
to do. A library is a big collection of procedures, where all the procedures are related! If you, say, want to control a
motor, you may want to find a Motor Control Library: a collection of procedures that have already been written for you
that you can use without having to do the dirty work of learning the nuances of motors.
For example, this is the Serial Library, which allows the Arduino to send data back to the computer:
Using Libraries
One of the best features of the Arduino project is the ability to add on pre-crafted libraries that add hardware support.
There's tons of them, and you can pick and choose which to install. They're only loaded in when the sketch you're
working on needs them, so for the most part you can download and stash them for future use.
Sketches will often depend on libraries, you can see what they are by looking at the top of the sketch. If you see
something like:
#include <FatReader.h>
That means that you'll need a library called FatReader or a library that contains the file FatReader. If you dont have it
installed you'll get an error:
There may also be some .o files. The .o files are C++ compiled Objects. If you end up working on the library and
modifying it, be sure to delete the .o files as that will force the Arduino IDE to recompile the modified .cpp's into fresh
.o's.
In v17 and up, the user libraries are now stored in the ArduinoSketchDirectory/libraries folder. You may need to make
the libraries sub-folder the first time. However, the good thing about this is you wont have to move & reinstall your
libraries every time you upgrade the software.
For example, here is how it looks when NewSoftSerial is installed in Windows (of course your username will be
different).
Check that the Documents/arduino/libraries/MyNewLibary folder contains the .cpp and .h files.
This version has a few fixes: first it integrates the 'no-wait' and 'no-hang' fixes below. It also fixes the annoying "missing
signature bytes" bug that freaks out avrdude when programming without the IDE. I also repaired the EEPROM code so
that now you can upload and download the EEPROM memory as well as flash. Finally, theres a 'upload feedback' using
the LED, for arduino clones that don't have TX/RX leds.
Please note that the fuses are different for this chip because of the extended memory!
"No-Wait" Bootloader
Here's a bootloader hack that will automatically start the sketch after it has been uploaded and will also only start the
bootloader when the reset button is pressed (so when you plug in power it will go straight to the sketch).
ch = MCUSR;
MCUSR = 0;
// Check if the WDT was used to reset, in which case we dont bootload and skip straight to the code. woot.
if (! (ch & _BV(EXTRF))) // if its a not an external reset...
app_start(); // skip bootloader
ch = MCUSR;
MCUSR = 0;
// Check if the WDT was used to reset, in which case we dont bootload and skip straight to the code. woot.
if (! (ch & _BV(EXTRF))) // if its a not an external reset...
app_start(); // skip bootloader
You can also just grab the source code and compiled hex file here.
No-Hang Bootloader
If you are using a Diecimila with auto-reset you may be frustrated when your communications program accidentally
triggers the bootloader. Here is a quick hack to make the bootloader quit if it doesn't receive a '0' character first (which
would indicate the Arduino software is trying to talk to it.
And paste:
Copy:
Paste:
} else if (firstchar == 0) {
// the first character we got is not '0', lets bail!
// autoreset via watchdog (sneaky!)
WDTCSR = _BV(WDE);
while (1); // 16 ms
}
You can also just replace the last two lines with app_start()
Just plug in the USB cable, then press the reset just before you start avrdude. If you need an avrdude tutorial, check
out this page.
Updating and replacing your Arduino is easy and painless and costs only a few dollars. Your sketches will work just as
before but with a little more breathing room.
In order to perform this upgrade you will have to either purchase a preprogrammed chip or program it yourself with a
AVR programmer or by 'bitbanging' it.
Next, prepare the new chip. The pins of ICs are a little skewed when they come from the factory, so they need to be
bent in just a tiny bit, to be parallel. Grip the chip from the ends and use a table.
If you purchased a chip from Adafruit that shipped before Feb 5, 2009 the chip will have the baudrate set at 19200
(same as the older Arduinos). After Feb 5 the upgrade chips were changed to 57600 baud rate (3 times faster!) in order
to be compatible with new Arduinos. If you have a 19200 baud rate chip you will have difficulty uploading. Simply quit
the Arduino application and edit the file in the hardware folder named boards.txt and change the line from:
atmega328.upload.speed=57600
to:
atmega328.upload.speed=19200
We use chips like the CD4050 to do level conversion but if you are using a lot of 3.3V devices, maybe you're just
better off upgrading the entire Arduino to run from 3.3V!
To do that, we will replace the regulator so that the DC barrel jack goes to a 3.3v type regulator, not a 5V. And then
reconfigure the 5V usb power line so it goes through the regulator as well.
To start, we'll need to remove the old regulator. The easiest way to do that is to first clip the two legs.
Then you'll need to heat the tab up to get it liquid so you can lift off the old part. Although it may seem counter intuitive,
its best to add some solder to the tab, melt it on with your iron, this will improve the heat conduction since the tab is so
large.
Now line up the new 3.3V regulator, and solder the tab first, use plenty of solder and be patient, the tab acts like a heat
sink.
The easiest way to make the USB 5V also go through the regulator is to remove the fuse and solder a diode from the
USB output to the regulator input.
You can use any power diode, a 1N4001 is perfect and only a few pennies.
The trade off is now there is no 500 mA fuse for the USB jack. The good news is that computers will have their own
fuses on the USB connector (inside the computer) so its not likely you will destroy your PC. But be aware that you're
losing a little safety.
Heat the fuse with your soldering iron, again adding solder may help thermal conductivity. Since the fuse is very
conductive you can probably just heat one side for a while and both ends will melt.
Clip the diode short and bend the leads over. Solder the side without a stripe (anode) to the old fuse pad, nearest the
board edge. Solder the striped end (cathode) to the right hand leg of the regulator.
The Arduino will still automatically select whichever power plug is giving you more power.
You can buy these from McMaster Carr part no. 95495K66 (in large quantities) or Adafruit
Open up hardware/cores/arduino (or cores/arduino) directory, and edit the file named wiring_serial.c or
HardwareSerial.cpp
Near the top is a #define RX_BUFFER_SIZE 128, which means 128 bytes are used for the buffer. You can change this
to 32 (or even 16!). If you have almost no serial input, make it as low as you'd like as long as its > 0.
You can also save another 2 bytes by changing rx_buffer_head and rx_buffer_tail from int to uint8_t
The good news is that you can burn bootloader using your existing Arduino with only a little bit of work. There's even a
minitutorial on the arduino.cc site.
This tutorial is an extention of that tutorial. First we'll show how you can make a permanent bootloader-burner by
soldering a 28-pin ZIF socket to a proto shield and use the PWM output line of the Arduino to generate a clock. This
will let you 'rescue' many chips that have been set to the wrong type of oscillator, or change ones that are set from
external oscillator (most Arduino bootloaders) to internal (such as the lilypad).
Parts
You will need…
An Arduino
A proto shield kit
28-pin ZIF (zero-insertion force) socket (you can use a plain socket but ZIF is ideal)
Some wire
Blank ATmega328P
If you bought the kit from Adafruit, you'll have an extra few items such as a Piezo beeper, LEDs, buttons, etc. that you
can use for the Standalone version of this project, just ignore them for now!
Assemble
First up, place the ZIF socket on the proto shield like so:
Pin 1 to digital 10 - Blue Don't forget to bend the wire over underneath to connect to the ZIF socket pin when
soldering!!!
Pin 7 to 5V - Red
Pin 8 to Ground - Black
Follow the protoshield tutorial to solder in the Red LED into LED1 position, Green LED into LED2 position. Also solder in
the two 1.0K resistors next to the LEDs. We'll use the LEDs as indicators. Then solder a wire from the LED2 breakout
(white) to analog 0 and a wire from LED1 breakout (white) to digital 8.
We have a report that this procedure does not work with Arduino 1.5.2. Use the latest mainstream Arduino
release instead!
Plug the shield on top, lift the latch, pop in the chip and then lower the latch. Make sure the chip orientation is like so
(so with the lever on the left side you can read the text):
With the USB cable still plugged in (and the same Serial port selected as before) Select Tools→Burn
Bootloader→w/Arduino as ISP
On newer versions of the Arduino IDE, select Arduino as ISP from the Tools→Programmer menu, then select Burn
Bootloader from the Tools menu.
Thats it! Don't forget, you can burn a few different kinds of bootloaders, such as Uno, Duemilanove, Lilypad so
depending on your situation you may want to use one over the other.
Instead of atmega328 you can also program atmega8 atmega88 atmega48 atmega168, etc.