Introduction: How to Use a Raspberry Pi's GPIO Pins and Avrdude to Bit-bang-program DIMP 2 or DA PIMP 2

These are step-by-step instructions on how to use a Raspberry Pi and the free open-source command avrdude to bit-bang-program a DIMP 2 or DA PIMP 2. I assume that you are familiar with your Raspberry Pi and the LINUX command line. You don't have to be familiar with AVR chip programming, but it helps if something goes wrong because you can read the output of avrdude and have some idea of what to do.

This procedure should take about 1 hour the first time and 5 to 10 minutes each time after.

You will need:

1) A Raspberry Pi with GPIO pins and power supply. I am using a Raspberry Pi Zero W with GPIO pins soldered on. The OS should be Raspberry Pi OS (formerly Raspbian). I am using Raspbian Lite (Stretch), which is kind of old. These commands should still work on the latest version of Raspberry Pi OS.

Going forward I will refer to the Raspberry Pi as "RPi."

2) Female-to-female jumper wires. Get one of each of these colors: red, black, blue, yellow, green, purple. I am using brown instead of purple.

3) A DIMP 2 or DA PIMP 2 with the optional 10-pin ICSP header soldered in. I am using a DIMP 2 here, but the instructions are the same for the DA PIMP 2.

4) Internet access for the RPi so you can install the free open-source command avrdude.

5) A copy of the .hex flash file for your DIMP 2 or DA PIMP 2. You can build the .hex file yourself from the raw source code, but I will not be showing you how to do that here.

6) Some way to log in to the RPi--use either a network connection or direct console access. You need to be able to run commands on it.

Step 1: Remove the AC Power Cord and Remove the 9V Battery

First, check if the AC power cord is attached to the DIMP 2 or DA PIMP 2 and plugged into the AC mains. If so, DANGER: LETHAL SHOCK HAZARD. Wear rubber gloves and then turn off the main rocker switch on the DIMP 2 or DA PIMP 2. Then, unplug the AC power cord from the AC mains, and detach the AC power cord from the DIMP 2 or DA PIMP 2. You can take off the rubber gloves once the AC power cord is removed.

Then, remove the 9V battery. Power will be provided to the DIMP 2 by the RPi through the red (Vcc) and black (GND) wires. Turn the slider switch on the DIMP 2 or DA PIMP 2 to off just in case.

Step 2: Hook Up the Jumper Wires

With the RPi not powered up, start hooking up the jumper wires. Going forward, the GPIO pins are on the Raspberry Pi and the ICSP pins are on the DIMP 2 (header J3) or DA PIMP 2 (header J1).

Refer to the diagram I cobbled together and the photos if you are lost.

The RPi portion of the diagram is from pinout.xyz.

The ATMEGA48V-10PU portion of the diagram is copyright 2016 Atmel Corp.

I am claiming fair use of these drawings due to the significant, transformative additions.

Black goes from GPIO pin 6 to ICSP pin 10. This is GND (Ground)

Yellow goes from GPIO pin 12 to ICSP pin 9. This is MISO.

Green goes from GPIO pin 16 to ICSP pin 1. This is MOSI.

Blue goes from GPIO pin 18 to ICSP pin 7. This is SCK or SCLK (SClock)

Purple (brown in my photos) goes from GPIO pin 32 to ICSP pin 5. This is RESET.

Red goes from GPIO pin 4 to ICSP pin 2. This is Vcc (5V Power)

Step 3: Power Up the RPi

Now, go ahead and power up the RPi. The voltmeter portion of DIMP 2 or DA PIMP 2 should power up too, but you won't know it by looking at the display. If in doubt, measure the voltage with the red probe at pin 20 on the ATMEGA48V-10PU and the black probe at pin 4, 6, or 8 on the ICSP header. It should be around 5VDC.

Step 4: Install Avrdude

My instructions will only show command line commands. If you have a GUI running, you will want to open a Terminal to run these command line commands.

Log in as the default user pi. The default password is raspberry

Install the avrdude command by typing the following at the terminal command prompt:

sudo apt-get install avrdude

Step 5: Edit the Avrdude Config File

Edit the avrdude config file by typing:

sudo nano /etc/avrdude.conf

Add these lines to the middle of the file where the other programmer definitions are. What I did is copy the programmer section right above it for id = "linuxgpio", then paste it right below (at line 1274), then edited the new section.

programmer
  id    = "pi_1";
  desc  = "Use the Linux sysfs interface to bitbang GPIO lines";
  type  = "linuxgpio";
  reset = 12;
  sck   = 24;
  mosi  = 23;
  miso  = 18;
;

Then save the file by pressing: Ctrl-O

And then quit the nano editor by pressing: Ctrl-X

Step 6: Download or Copy the .hex Flash File to /home/pi

Copy the .hex flash file to the RPi. I assume you know how to do this. HINT: Use the wget, curl, git, or scp command to get the file down from the web to the RPi.

DIMP 2's .hex file is here, along with the source code:

https://github.com/dchang0/dimp2

NOTE: The dimp2.hex file works with older versions of avrdude but does not work with more recent versions. It produces this error:

avrdude: reading input file "dimp2.hex"
avrdude: input file dimp2.hex auto detected as Intel Hex
avrdude: ERROR: address 0x1010 out of range at line 257 of dimp2.hex
avrdude: read from file 'dimp2.hex' failed


Use dimp2.ino_atmega48_1000000L.hex for newer versions of avrdude. I have confirmed it to work with avrdude 6.3.


A customized version of DA PIMP 2's .hex file is here.

https://github.com/jcwren/DaPimp2

If the above version of DA PIMP 2's .hex file doesn't work, Mikey Sklar's original DA PIMP 2's source code is here. You will have to compile it into a working .hex file yourself. I don't cover that in these instructions because it will be a lengthy (but not difficult) tutorial. I did successfully program several working DA PIMP 2 units using this source code:

https://drive.google.com/open?id=0Bx5Als-UeiZbSUdH...

Or you can go to the main DA PIMP 2 page here and click on the link to the source code.

https://mikeysklar.blogspot.com/p/da-pimp-battery-...

Put the .hex file at this path and filename on the RPi...

For the DIMP 2:

/home/pi/dimp2.hex

or

/home/pi/dimp2.ino_atmega48_1000000L.hex

For the DA PIMP 2:

/home/pi/da_pimp2.hex


Step 7: Verify That Avrdude Can Talk With the ATMEGA48V-10PU

Run the avrdude command to make sure it can talk with the ATMEGA48V-10PU chip on the DIMP 2 or DA PIMP 2.

cd /home/pi

sudo avrdude -c pi_1 -p m48 -v

You'll get about a page of output. Look at the end. If you get a response like this, then you can proceed.

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

If not, then something is wrong and you should figure it out. Most likely it is a miswiring, but another common complaint I've seen is that the ATMEGA48V-10PU chip is a fake. It seems that many of them sold on Amazon or ebay are fakes. The ones sold by Mouser or Digikey and other authorized distributors are genuine.

Step 8: Flash the .hex File to the ATMEGA48V-10PU

Run this command to do the programming of the chip...

For the DIMP 2:

cd /home/pi

sudo avrdude -c pi_1 -p m48 -U flash:w:dimp2.hex

or

sudo avrdude -c pi_1 -p m48 -U flash:w:dimp2.ino_atmega48_1000000L.hex

For the DA PIMP 2:

cd /home/pi

sudo avrdude -c pi_1 -p m48 -U flash:w:da_pimp2.hex

You'll get about a page of output. Look for these lines:

avrdude: writing flash (1528 bytes):

Writing | ################################################## | 100% 0.79s

...

avrdude: verifying ...
avrdude: 1528 bytes of flash verified

avrdude: safemode: Fuses OK (E:FF, H:DF, L:62)

If you got this far, you should see the LED display showing zeroes for the voltage. If you see the zeroes, you're done! If not, skip to the next step.

If you see the zeroes, gracefully shut down your RPi with this command:

sudo shutdown -h now

When the power light on the RPi turns off (the DIMP 2 or DA PIMP 2 will still be on), you can unplug the power supply from the RPi. Then unplug the jumper wires between the RPi and the DIMP 2 or DA PIMP 2.

Step 9: Simple Troubleshooting If the Flash Failed

If you don't see zeroes on your DIMP 2 or DA PIMP 2's display, then it's time to troubleshoot.

Again, check your wiring first.

Then, check the fuse states shown by avrdude. It is possible that your chip came with the fuses set to different values than the factory default. The chip might need to have its fuses reset, which requires a totally different hardware device. This is a problem with many of the fake ATMEGA48V-10PU chips sold on ebay--they are old chips pulled from discarded hardware, and the fuses have been set, and the seller did not bother to reset the fuses.

Sometimes the chip is not even an ATMEGA48V-10PU. It could be a different chip relabeled. Usually you can tell these fakes by looking carefully at the markings on the top and bottom of the chip. It's wiser to get your ATMEGA48V-10PU through a trusted supplier like Mouser or Digikey.


If you get this error:

avrdude: ERROR: address 0x1010 out of range at line 257 of dimp2.hex

use the newest .hex file, which should be:

dimp2.ino_atmega48_1000000L.hex


If you do see the letters "D I M P" and then zeroes but they are cycling very slowly, one digit at a time, this is because your ATMEGA48 is running at something other than "Internal 1MHz oscillator.