Category Archives: electronics

Introducing: Carduino, a micro for Car hacking.

Becoming a member of Wyolum has it’s privileges (and responsibilities)! While waiting for my AlaMode boards to come back, I’ve been helping out on another project led by David Pincus, with electronics and layout by Anool Mahidharia.

David originally wanted this to supply power to his new in-car DVR that has the capability of recording triggered by motion detected by it’s on-board accelerometers. He was worried that if he didn’t drive the car for a few days, it might drain the battery. This little Arduino compatible board can monitor two voltages (through a precision resister voltage divider) and shut off a solid state relay if the voltage falls below a selectable value. There’s also an optional display board (shown here displaying the supply voltage). If you don’t want to use the display, the row of pins is a handy breakout of many of the unused Arduino pins.

 

 

 

Other features:

  • i2C port (sensors anyone?)
  •  2.5 A solid state relay.
  • Jumper block for setting shutdown voltage. Here we got a bit clever, using a resistor ladder tied to an analog pin. Uses just one pin but multiple values.
  • FTDI and ISP programmable
  • Can be powered by FTDI or VBATT input (up to 15V)
  • Protection diodes on all the power inputs
  • Screw terminals for easy connection.
  • Easy to assemble through hole kit.

We’re making a few tweaks, and then we are thinking about doing a kickstarter to judge interest. We’d love to hear what you’d do with it, I think the applications aren’t limited to cars. Certainly, boats, RV’s and maybe solar applications also come to mind.

I’ve learned a lot on this project. I did the logistics, ordering the boards from Laen (http://oshpark.com, Dorkbot PDX’s new online submission system), kitting parts and writing some test code and trying out the first version.

If you are interested, give us a shout (either on my google+ stream, or at the wyolum.com forums.)

Raspbery Pi à la mode ?

Lot’s of people are excited about the Raspberry Pi®, a complete computer for $25 (or $35 with ethernet).
The initial shipment is entirely sold out, and there are 200,000 more on order. Lots of people have been saying that this is the end of the Arduino, as it’s about the same cost, a full linux system with keyboard, mouse, and hdmi video output.

I think this is an oversimplification. Arduino is fantastic at interfacing to the physical world in a way that no linux (or Windows) PC could hope to approach at any cost.

The Pi does have a GPIO connector, but it pales in comparison to the humble Arduino. No built in PWM (servos anyone?) and no Analog to digital conversion.

Why throw the baby out with the bathwater? How about some delicious ice cream with your Raspberry Pi?
As a member of the amazing Open Source Hardare collective Wyolum.com,  I’m working with Justin Shaw and  Anool Mahidharia on “à la mode”: an Arduino clone specifically for the Raspberry Pi (That rendering above is courtesy of our  fabulous  EE  Anool) . You can of course connect an Arduino to a Pi USB port, but when you want a turnkey solution, how about an Arduino compatible “plate” (what the Pi folks call shields) that fits right on top of the Raspberry Pi.

You can plug your existing shields right in,  and you can even run the Arduino IDE on the Pi. Since the Pi’s core mission is to provide equal access to computing to all children, this will also give the kids the opportunity to mix it up “Arduino-style” with real world hardware.

We couldn’t have a vanilla add-on, so we added:

  • A battery backed real time clock: RTC: DS3231 (same as ChronoDot)
  • uSD card for logging
  • Socketed Atmel Atmega 328p in case you make a wiring mistake and need to replace it.

We’re sending off for prototype boards soon, so if you have feedback, let us know! Join the conversation at the Wyolum.com forums.

Raspberry Pi® is a trademark of the Raspberry Pi Foundation.

Arduino Library for Scratch Sensor Board Emulation

I re-wrote the simple sketch as an Arduino Library for Scratch Sensor Board (picoboard) emulation.

You can download it at github

Unzip the contents of this repository into a directory called ScratchSensors in the Arduino libraries directory (or in Sketchbook/libraries)

In your sketch: 

#include <ScratchSensors.h>

and then: 

ScratchSensors Scratchboard;

In Setup: 

Scratchboard.init();

Sensor values are stored in an array internally to the ScratchSensors class called 

Values[];

The picoboard/Scratch sensor board that is emulated has specific sensor labels that are reported:

#define RESISTA 0
#define RESISTB 1
#define RESISTC 2
#define RESISTD 3
#define SLIDER 4
#define LIGHT 5
#define SOUND 6
#define BUTTON 7

Values should be between 0 and 1023. These are scaled by scratch to be between 0 and 100. BUTTON is handled in a special way: 0 is depressed and 1023 is not pressed. Let’s say you have an analog sensor like an accelerometer connected to Arduino pins A0, A1, and A2. Then during loop():

Scratchboard.Values[RESISTA] = analogRead(A0);
Scratchboard.Values[RESISTB] = analogRead(A1);
Scratchboard.Values[RESISTC] = analogRead(A2);

then, write the packets out to the serial/usb port: 

Scratchboard.report();

There’s an example in the examples directory based on the Sparkfun midi shield. It has two pots and three buttons. Of course only one of the buttons can be used with the Scratch buttonpressed semantics, but the others can be tested for specific values.

Interfacing Scratch to the Arduino Platform

I’m a big fan of Scratch, the educational programming language created at MIT. International Scratch Day is coming up, and when we went two years ago, we had a blast. Last time, I added a distance sensor to an Arduino, made it emulate a Scratch Sensor Board, and  put it under a trampolene. This year, I decided to document the general process of making your own scratch sensors and bring a compact sensor module for the kids to play with.

At one point during our research at work, we needed a way to measure the movement and relative distance of a camera platform, so I designed, and my tech put together a combination of a Teensy (Arduino Compatible microcontroller), an Adafruit ADXL335 Accelerometer breakout, and a Sharp IR distance sensor.

I dug up my old code for the trampolene hack, which was based on some code I got off the Scratch forums. It didn’t work!

What we are actually doing is emulating what was originally called a “Scratchboard” and now produced by a company called the Playful invention company and called a “PicoBoard”.

There’s a spec (and schematics) at http://info.scratch.mit.edu/sites/infoscratch.media.mit.edu/files/file/ScratchBoard_Tech_InfoR2.pdf

Turns out, the platform used to be more forgiving, and now it expects a report of all the sensors (whether we are using them or not.)

The IR distance Sensor outputs an analog voltage proportional to the distance to the nearest thing in it’s path, and likewise the accelerometer outputs a voltage proportional to the acceleration (including gravity). I mapped the distance sensor to the “slider” sensor, and the X, Y and Z outputs to the Resistance A, B and C sensors.

Here’s a demo video:

I’m planning on re-working the interface code into an Arduino Library, and I’ll post that here when I get it done.

In the meantime here’s the Arduino code:

// Scratchboard emulation
// by Kevin Osborn
// http://baldwisdom.com
// Feel free to use this code in any way you wish! (Though if you want to link
// to my blog, that would be cool)
// The Scratchmote has a sharp ir distance sensor
// and a 3 axis accellerometer

#define DISTSENSE A0
#define ACCELX    A1
#define ACCELY    A2
#define ACCELZ    A3

#define TESTMODE  0

// Format output for ScratchBoard emulation
// sensor=0-7, value=0-1023 (rescaled by Scratch to 0-100)
// 0="A", 1="B", 2="C", 3="D",
// 4="Slider", 5="Light", 6="Sound", 7="Button"
void ScratchBoardSensorReport(int sensor, int value)
{
  Serial.write( B10000000
                 | ((sensor & B1111)<>7) & B111));
  delayMicroseconds(400);
  Serial.write( value & B1111111);
}
int flatx,flaty,flatz; // might be used to set midpoint
int maxX,minX; //testing purposes
int lastx,lasty,lastz;

void setup()
{
  pinMode(DISTSENSE, INPUT);
  pinMode(ACCELX,INPUT);
  pinMode(ACCELY,INPUT);
  pinMode(ACCELZ,INPUT);

  lastx =maxX= minX = flatx=analogRead(ACCELX);
  lasty = flaty=analogRead(ACCELY);
  lastz = flatz=analogRead(ACCELZ);

  Serial.begin(38400);
}

void loop()
{
  int distance = abs(map(analogRead(DISTSENSE),0,650,0,1023)-1023);
  //experimenting with the ADXL335 gives us readings between low single digits
  // and < 700 for human powered movements   int x = map(analogRead(ACCELX),0,700,0,1023);   int y = map(analogRead(ACCELY),0,700,0,1023);   int z = map(analogRead(ACCELZ),0,700,0,1023);      int rx = (x +lastx)/2;   int ry = (y +lasty)/2;   int rz = (z +lastz)/2;      lastx = rx;   lasty = ry;   lastz = rz; #if TESTMODE Serial.println(distance); /* Serial.print("x,y,z= "); Serial.print(x);Serial.print(","); Serial.print(y);Serial.print(","); Serial.println(z); */ if (x > maxX) maxX=x;
if (x< minX) minX=x;
Serial.print("maxX = ");Serial.println(maxX);
Serial.print("minX = ");Serial.println(minX);
delay(500);

#else
if (Serial.available())
{
  if (Serial.read() == 0x01){
  ScratchBoardSensorReport(0,rx);
  ScratchBoardSensorReport(1,ry);
  ScratchBoardSensorReport(2,rz);
  ScratchBoardSensorReport(3,0);
  ScratchBoardSensorReport(4,distance);  
  ScratchBoardSensorReport(5,0);
  ScratchBoardSensorReport(6,0);
  ScratchBoardSensorReport(7,0);
  }
 //ScratchBoardSensorReport(1,analogRead(POT2));
 // ScratchBoardSensorReport(7,digitalRead(BUTTON1)?0:1023);
  delay(30);
}
#endif

}

The Scratch examples are on the scratch site:

Note you can’t play them directly from the site, as you need the sensors!

 

Soldering Tips from Dorkbot PDX’s Scott Dixon

I love google+ it often exposes me to really amazing people and projects. Even better, sometimes when you comment, people are very generous with their help. I recently saw a post on Dangerous Prototypes blog about a group build of one of their the Bus Pirate V4. They laid out the parts and did two shifts to build 30 copies of the board.

It was done at Dorkbot PDX (People Doing Strange Things with Electricity in Portland Oregon), and I noticed that they were using a really cool PID controlled hotplate for surfacemount soldering, using a hacked Harbor Freight IR thermometer, and an optimized for the task Arduino clone.

I commented (Google +) on one of the pictures taken by Scott Dixon, one of the organizers, and developers of the soldering system with some questions.  The conversation that ensued was very informative and I wanted to share it with you:

Kevin: I’m newish to SMD, but I’m finding in some ways it’s easier than through hole. No more trying to get things flat, etc.

Scott: Definitely.  Also no turning the board over all the time to solder the leads, etc…

Kevin: Someone showed me how to use a toaster oven, but a lot of folks I know use the hotplate. Is one better than another?

Scott: For boards with parts primarily on one side, the hot plate is simpler and works fine.  I haven’t used a toaster oven but I hear some models work quite well particularly when you want to reflow both sides at once.  That was one of the reasons I put the thermocouple interface on the board but so far we haven’t used it for that.  Also, some day I might try sous vide.

Kevin: I recently built my first all SMD board. I did it by hand, with a little guidance from an experienced technician. Is there a time when it’s better to use one technique or the other?

There is an interesting cross over point between hand soldering and hot plate reflow depending on the complexity of the board (and how good you are at hand soldering).  As an experiment, three of us hand soldered the Bus Pirate.  We all have pretty good equipment, including stereo microscopes and we are all pretty good at hand soldering (although nowhere in the league of a tech who does it all the time).  It took each of us about 2 hours to do the BPV4 by hand.  By comparison, the first time we did the BP on the hotplate it took about 45-50 minutes or so (including getting the parts off the reels, etc).  We had a solder paste stencil so that made it somewhat faster.  So for something with that many parts it was over twice as long to hand solder.  A simpler project would be closer and a really simple project would be faster by hand, compared to setting up the hotplate and cleaning the stencil, etc.  If you don’t have a stencil and have to put the solder paste on by hand, that is somewhat slower.

I guess the only other thing I’d add is that we have done a number of SMD workshops and training sessions and our success rate for people walking out with a working project is often 100% and never very much below that.  So I would encourage people to give it a try.  It is almost certainly easier than you think.  It helps if you can find a workshop or class the first time but you can certainly learn on your own.

It doesn’t take that much specialized equipment:  Some moderate magnification (3X or so,  like reading glasses or a hands free magnifying glass) and a good pair of fine point anti-magnetic tweezers.  Maybe a 10X or so magnifier for inspection.  If you don’t want to spring for a temperature controlled hot plate or toaster oven, you can certainly make do by manually controlling the heat.  Having a temperature control system does reduce the stress though and decrease the likelihood that you will overcook some parts.

And, at least at first, I’d recommend using professionally produced PC boards with solder mask.  That helps to control the solder and minimizes solder bridges.  DorkbotPDX is a great place to get high quality inexpensive PC boards made!

Oh, and one more thing:  If you are putting solder paste on by hand (without a stencil) you will almost certainly err on the side of depositing way too much paste.  Think about the fact that a stencil only leaves a solder paste layer a few mils thick and how little paste that actually is.

Ahoy! Bus Pirating GPS

As a new member of Wyolum.com, I’ve been pitching in on their (our?) I2GPS project (Now available for pre-order!).

Wyolum has a terrific LED array board, originally intended to build word clocks, but Justin and others have also built a large format digital clock/timer display out of several of these boards, and the Super Accurate GPS clock will make it a terrific Race Clock.

I wanted to test out some GPS modules from an Asian source, and I felt that whatever Arduino code I had time to write was bound to be incomplete, so I decided to use the manufacturer’s PC progam.

We’re using the Fastrax UPS501, and the software is called Fastrax Workbench. The UPS501 is a terrific module, very sensitive (gets a good fix and tracks 9-10 satellites from within my house!) The reason Justin and Anool picked it, though, is that it has a 1 PPS output. This one Pulse Per Second can be used to get microsecond accuracy by lining this up with the GPS time as well.

Although it’s trivial, I thought I’d share how I used Dangerous Prototype’s Bus Pirate to hook up the GPS module to a PC for testing.

Since the UP501 GPS module communicates via TTL Serial, I wanted to use the Bus Pirate in “Transparent UART Bridge” mode.

I used this pinout chart to determine what was needed: http://dangerousprototypes.com/docs/Common_Bus_Pirate_cable_pinouts

  • Pin 1 is RXD on the GPS and this goes to TXD on the Bus Pirate: Grey wire or MOSI
  • Pin 2 is TXD on the GPS and this goes to RXD on the Bus Pirate:  Black wire or MISO
  • Pin 3 is GND on the GPS to GND on the Bus Pirate:  Brown Wire
  • Pin 5 and 6 are power and backup power, hook both those up to 3.3v Red wire on Bus Pirate
The bus pirate talks at 115200 baud. connect with your favorite terminal program (I’m using Teraterm Pro). Here’s the transcript of the commands to set it up (the semicolon and the comment following are not to be typed)
HiZ>m    ; Mode
1. HiZ
2. 1-WIRE
3. UART
4. I2C
5. SPI
6. 2WIRE
7. 3WIRE
8. LCD
x. exit(without change)
(1)>3     ;UART
Set serial port speed: (bps)  
 1. 300
 2. 1200
 3. 2400
 4. 4800
 5. 9600            ; Default baud for the GPS
 6. 19200
 7. 38400
 8. 57600
 9. 115200
10. BRG raw value
(1)>5      ;9600
Data bits and parity:
 1. 8, NONE *default
 2. 8, EVEN
 3. 8, ODD
 4. 9, NONE
(1)>      ;default - 1-8-none
Stop bits:
 1. 1 *default
 2. 2
(1)>              ; default 1
Receive polarity:
 1. Idle 1 *default
 2. Idle 0
(1)>           ;default 1
Select output type:
 1. Open drain (H=Hi-Z, L=GND)
 2. Normal (H=3.3V, L=GND)
(1)>2      ;Normal
Ready
UART>W          ; Turn on the power supplies
POWER SUPPLIES ON
UART>(1)    ; Select the transparent UART Bridge. If you forget this, you can type (0) for the options
UART bridge
Reset to exit
Are you sure? Y

Then it will start spitting out NMEA sentences

$GPGGA,235957.037,,,,,0,0,,,M,,M,,*43
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPGSV,1,1,00*79

Exit the program and fire up FastTrax Workbench. The only tricky part here is to select the correct baud rate. Since the Bus Pirate talks to the host at 115200 Baud, and the GPS at 9600 baud, we use 115200.

Debut of the DuckyBots!

Charlotte, Mason and I hopped into Cambridge last Friday for the Mini Maker Faire at the Cambridge Science Festival. They helped me set up a table to introduce DuckyBots! to the attendees. We had probably close to 500 kids (and a few adults) making ducky bots in the 4 hours of the Faire, and almost got into trouble because people didn’t want to stop. We were the last ones to shut down. An unqualified success! (more details after the picture)

Photo by Chris Conners

The idea for duckybots started from a conversation at the Boston Robotics Meetup with Meredith Garniss.  In addition to trying to involve kids in robotics, we thought it would be good to inspire the adult robotics crew to create for kids.

Programs like FIRST robotics, and school programs are already full of people interested in STEM and robotics, and we wanted to find something that would turn kids on to the joys of Science and Engineering.

The basic idea is to use turning rubber ducks into robots as a platform for experimentation, creativity and learning.
We thought we could apply this at many ages and levels. Really young kids can exercise their creativity by decorating ducks (drawing, gluing on.)

The middle level would concentrate on mechanical physics, making ducks move, maybe racing (but also have to offer non-competitive challenges for kids scared of  competition.)

To keep the ideas coming in, we can also have an advanced level, perhaps Sumo ducks (on water!)

For their first appearance, I needed an activity that could be be done by hundreds of kids in a short period of time, so I focused on “making them Move”

Rather than presenting them with a blank slate, I put together some modules that represent different propulsion types.

I created a fan unit from a toy motor, a AA battery box (with switch) and a fan propeller sold as a spare for snap circuits.

I also had some playmobil underwater motors, and lots of Duck(!) tape (especially yellow).

As kids strapped on the motors, and found the ducks tipping over,  presenting an engineering challenge! I also had some closed cell foam (from packing materials) that they could use for floats, and outriggers.

There was lots of inventing, trying, doing things “wrong” (turns out those air fans work fine underwater!)

I was thinking that this was a good test to see if I should spend any more time on this, and I think the answer is yes!

These ducks have legs!

Photo by Chris Conners

 

April Fools

About a week ago, Sparkfun posted a great prank in their weekly new product video. As April 1 was coming up, and I thought the kids would enjoy it, I ordered the bits that I didn’t have, and managed to pull it off.  Sparkfun didn’t give many details, so I thought I’d share my implementation.First here’s a video showing my wife getting pranked. She was a super good sport, (even not minding posting a video of her in her bathrobe!)

NOTE: Someone on google+ pointed out that at 8 amps, these horns could seriously heat up the li-po, and a hot li-po is not a good thing (fires/explosions!) so do this at your own risk. I only pulse the horns for 1 second at a time (twice per activation) so the battery never gets hot, but if you had a programming error, and it was stuck on, look out! Another good reason for the cutout switch and testing without the horns connected.

Here’s what you’ll need for this prank:

  1. Arduino
  2. Accelerometer: I used this one from ModernDevices
  3. Digitally controlled relay. I used the same one they used in the Sparkfun prank
  4. The Horns!
  5. A (nearly) 12V lightweight battery. Sparkfun has this nice 1500 MAH 11.1 LIPO for a good price
  6. A compatible charger. Sparkfun recommends the IMAX B6 This can also be found on Amazon
  7. Wires, connectors, a good way to connect all the parts together

.

Here I wired the battery to power the Arduino, one leg of power went directly to both horns, and the other switched through the relay. I also put a toggle switch inline  with the power to the horns so I could shut off the horns for testing (The relay has an led and also clicks, so you can tell if it’s operating).

I had a proto-screw-shield on hand which was convenient for hooking up both the accelerometer and connecting to the relays.

Here it’s almost all hooked up. The relay connections are made to the analog side of the arduino to simplify wire routing. (Both 5V ground and io pins together.)

I use the button and LED on the protoshield to arm the device and indicate it’s status.

The bolts on the horns were too short to go through 1/4 ” plywood, so I used the supplied mounting plates to offset mount the horns.

Here it is all put together.  Here’s the Arduino sketch, but I recommend you try to program this yourself!

LCDBootDrive: Selectable Files!

New toy (o-o-o)

After I showed the BootDrive on the Adafruit Show and Tell, I watched their weekly “Ask an Engineer” show where they showcased a new product, an LCD shield with an RGB backlight. The really cool thing is that the LCD AND 5 buttons only need 2 IO pins on the Arduino, because the shield is designed with an I2C expander. I ordered it the next day (close to 5pm) selected UPS ground shipping and it was at my house the next day!

Here’s a quick video before I go through the blow-by-blow:

I soldered the micro-sd breakout onto an Adafruit Proto Shield, and a 6 pin right angle header to connect an FTDI cable. Adafruit also had a pre-made 6pin-6pin female cable that was perfect for this project!

I wired it with fine wire-wrap wire on the bottom

The wiring is almost identical to the original BootDrive, except I changed the reset pin assignment:

Digital Pin5  –> FTDI Pin 6 (CTS)
GND                –> FTDI Pin 1 (GND)
Digital Pin1 (RX) –>FTDI Pin 4 (TX)
Digital Pin2 (TX)–>FTDI Pin 5 (RX)

SD Card Interface:
Digital Pin 10 –> SD-breakout CS
Digital Pin 11  –>  SD breakout DI
Digital Pin 12  –> SD breakout DO
Digital Pin 13  –> SD breakout CLK
GND                    –> SD breakout GND
5V                       –> SD breakout 5V

Debugging

I incorporated the directory listing code from the SD library examples, and instead of listing all the files, I did an openNextFile() each time the “down” button was pressed. It would be nice to be able to scroll up and down, but I’m pretty tight on memory as it is, so I didn’t want to cache the directory, or do some kludgy re-reading the whole directory to get to the file we are on.

The first problem I ran into was it worked fine until you scrolled through what turned out to be 25 names (actually the same 3 names 8+ times) and then it started repeating the same name over and over. When I realized it was always 25, I knew it wasn’t some overflow, and I finally figured out the example code had a bug in it! They list the directory by opening each file (with openNextFile() to get the name, but they never close it! If you try the listfiles example with more than 25 files on the card, you’ll see only the first 25. Easy enough to fix, as I’m scrolling I close the file after I get it’s name. I reopen it if you press the “select” or program button.

The next problem was a bit harder. The hex programs seemed to work on some boards but not others, even though there were apparently no errors. I created new test programs and suddenly they didn’t work either. Since the writing to flash seemed to be working, I figured that there must be some corruption of data. I read back the programmed AVR code from the target arduino (with avrdude  <programmer parameters….> -U f:r:readfile.hex:i) and compared it with the file I was sending to be programmed. It turns out I was advancing to the next “line” of 16 bytes even when I read less. Files that ended in shorter lines were getting placed ahead of where they needed to be and so the program was corrupt. Probably on some, the data that was already there was harmless, or the program didn’t actually execute that part of it’s memory, but in some cases it clearly was. Fixed, putback to github!

Have fun and let me know if you try it!

https://github.com/osbock/Baldwisdom/tree/master/LCDBootDrive

Precious Memory

There are two kinds of memory(well three including EEPROM, but we won’t deal with that here) you can run out of when doing an Arduino project (or any embedded project):

  1. Flash Memory: For a lot of applications, the Arduino has a fairly big flash memory (the UNO and all atmega328 based Arduinos give you 32k flash.) The Arduino IDE will also tell you if your program size exceeds the amount of flash in the particular Arduino you have selected.
  2. Static RAM: Atmega328 in the UNO gives you 2k of RAM. This is the most precious commodity in Arduino programming, and it’s often surprising how fast you can run out. The bad thing is,  the IDE won’t warn you, and your program can go from working to behaving really weirdly by doing something as simple as adding an additional debug print statement. This post will attempt to explain this, and give you a few tips for understanding your RAM use.

Where Did it All Go?
When you run out of SRAM, your program can just stop working, for reasons ranging from one data structure overwriting another (so your program does something based on the wrong value) to overflowing into the stack. When that happens, generally, your program dies a horrible death, because returning from subroutines can cause the processor to jump to random places in memory. If you make an innocuous change to your code (like adding another debug statement) and your program suddenly behaves very differently, chances are you have run out of SRAM.

It’s obvious if you declare large variable arrays like:

int valueArray[1024];

would instantly overflow memory because int’s occupy 2 bytes in atmega land. You also need to leave room for stack, and any other variables your libraries, and the Arduino software itself uses.

What’s Harvard got to do with it?
Not so obvious is the fact that:

Serial.println("Hello");
const char constantString[] = {"Hello"};

both  occupy 6 bytes in both flash memory and in SRAM (c-strings are terminated with a Null, or 0x00 byte).  The reason is AVR 8 bit microcontrollers use a Harvard architecture addressing scheme. This means that when an instruction addresses memory, there are two versions, one that addresses program memory (flash), and another to address RAM. Most routines like Serial.println use regular RAM, as they don’t know if you’ll be passing in constant info or stuff that you’ve constructed. As a result, even static strings (with a const declaration) are copied from flash to SRAM at system startup. It’s possible to write routines to only use flash (using the PROGMEM attribute) and it’s definitely useful, but that’s covered elsewhere.

I ran into this porting the avrdude stk500 routines to Arduino, blindly converting the printfs into Serial.print statements. In order to get enough RAM I ended up using numeric codes for errors (I’ll probably eventually get around to PROGMEM-ing them).

So, how do you tell if you are nearly running out, or have already? The IDE won’t tell you, so you have to dig in a little.

The build process for arduino produces an “ELF” file or Executable and Linkable File. This is used to create the actual HEX file for programming, but still has memory segment information embedded in it.

First find your build directory. On Windows 7 this is \users\<youruser>\AppData\Temp\build<a bunch of numbers>.tmp

Now, assuming you have the avrdude files somewhere in your path, use the command:

avr-size -C --mcu=atmega328p <yourelf-file.elf>

For example, the current version of Bootdrive gives me:

avr-size -C --mcu=atmega328p BootDrive.cpp.elf
 AVR Memory Usage
 ----------------
 Device: atmega328p
Program: 19272 bytes (58.8% Full)
 (.text + .data + .bootloader)
Data: 1340 bytes (65.4% Full)
 (.data + .bss + .noinit)

It’s probably best not to go beyond 95% full for your Data (that’s the SRAM part) to save room for your stack, and even less if you pass big variables on the stack or have deep call chains.