Sunday 22 July 2018

Batteries, PICkit2 and ATmega328 (Pro Mini)

I've had a need to reprogram the fuses on an Arduino Pro Mini clone, as I'm intending to use it on battery power. Running on a couple of AA batteries means the highest voltage the device is going to see is 3v, rather than the 5v the device is expecting. This is going to mean a few modifications in order to make this all work! Just in case you thought that wasn't stacking things up enough, I'm going to be using my PICkit2 to program the Pro Mini clone. 
Now, there are a few things that need to be done in order to make this all work. The first is reducing the power consumption of the device! There are some features of the Pro Mini clone that might be great if you're powering it from a solid power source, but work against me if I want a decent battery life. These include a permanently powered LED, which is burning power up all the time, and a voltage regulator that'll be fairly pointless. Removing these two will reduce the power consumption of the device.

The second is that the bootloader settings and BrownOut Detector (BOD) need to be changed to better suit this configuration. In order to do this, I need something that'll act as the interface between the PC and Pro Mini. Rumour has it that AVRDude supports the PICKit2 programmer, which has been a fairly reliable staple over the years.

If you have experience with components, it should be fairly obvious where the voltage regulator lives. If you're not sure, then start with the power input (RAW on the Mini Pro), and trace the tracks on the board. The first component looks to be some kind of polyfuse, then a diode, and finally a chip with five pins, marked LG50. Voltage regulators usually have three or more terminals, and that is a giveaway on a board this simple!


In the picture above, I've circled the voltage regulator (LG50), a decoupling capacitor, the power LED, and a resistor for the power LED (marked 102). Normally I'd grab a hot air gun to work with surface mount components, but in this case, it is simple enough to do with a larger soldering iron tip. I worked on removing the voltage regulator first by heating the three legs with the soldering iron and gently hinging the part upwards on the remaining two legs. Having lifted one side free, I then repeated the process on the other side to remove the regulator from the board. I don't need to keep the voltage regulator, so I'm not too concerned if I kill the part.

Removing the resistor for the LED is as simple as placing the soldering iron tip across the top of the resistor so that I heat both sides of the package. Then, when I remove the soldering iron, the resistor tends to stay on the iron, removing it from the board. Again, I'm not too concerned about keeping the part. After this, you should end up with a board that looks like:



You may note that I'd forgotten to take a picture until after I'd soldered on a female header for the serial port!

Now that the hardware modifications have been made, it is time to turn my attention to the bootloader and fuses. However, it would be wise to get the PICkit2 setup. Based on information found in the AVRDUDE manual, it seems that the following should be an appropriate pinout.

PICKit2Pro Mini
1 (VPP)Reset
2 (VDD)VCC
3 (GND)Ground
4 (PGD)12 (MISO)
5 (PGC)13 (SCLK)
6 (AUX)11 (MOSI)

Fortunately, all of these pins are available on one side of the Pro Mini, so I threw together a quick adapter to go from the six pin PICKit2 connector to a 12 pin 0.1" strip for one side of the Pro Mini pinout. Connected up, it looks like this:



If you're using the Arduino IDE or the Visual Micro plugin for Visual Studio, then you'll also need to add four lines to the programmers.txt file. If you're on Windows, then you should be able to find the file at C:\Program Files (x86)\Arduino\hardware\arduino\avr. These lines just add support for the PICKit2 to the IDE, meaning you can select the PICKit2 from within the IDE, rather than having to run AVRDUDE from the command line. This is useful if you're running MiniCore, as it makes it much easier to manage the bootloader and fuses. I find being able to select the options I want much easier than having to calculate the fuses and find the right Optiboot file!

pickit2.name=PICkit 2
pickit2.communication=usb
pickit2.protocol=pickit2
pickit2.program.protocol=pickit2
pickit2.program.tool=avrdude
pickit2.program.extra_params=

Now that we have the PICKit2 tool enabled, we can start the IDE, select our options and burn the fuses/bootloader into our device. If you're using the Arduino IDE, make sure you set the Board to a suitable MiniCore board, set the various options, set the programmer, and then finally Burn the Bootloader!



On the other hand, if you're using vMicro, it should have been a fairly similar process. Set the board and the options. Set the programmer to PICKit 2, and hit Burn Bootloader. However, there's a bug within vMicro that results in the programming failing. It has been reported, and VisualMicro has been working to correct this.



You may notice that the new settings configure the device to operate at 8MHz instead of 16MHz. There are two reasons for this. The main reason is that 16MHz is outside specifications at 3v and below. The secondary reason is that the higher speed will consume more power, and I don't need that extra performance.

One challenge with running with an 8MHz internal oscillator is that it isn't accurate enough to maintain a high baud rate. MiniCore state that:

There might be some issues related to the internal oscillator. It's factory calibrated, but maybe a little "off" depending on the calibration, ambient temperature and operating voltage.

For me, it was off enough to break sketch uploading. Attempting to upload a sketch just resulted in timeouts from AVRDUDE, as it couldn't communicate. I was tempted to try to measure the actual clock speed so I could work out a suitable baud rate but chose the easy option instead. I ended up modifying the MiniCore boards.txt to set the baud rate to 19200. On Windows, you need to edit %appdata%\..\Local\Arduino15\packages\MiniCore\hardware\avr\2.0.1\boards.txt. There's a handy baud rate to clock speed chart at the start of the file. Just find your combination of chip and clock, then modify the upload.speed line. In my case, I changed it to be:

328.menu.clock.8MHz_internal.upload.speed=19200

After that change (and rescanning for toolchain in the IDE), I was able to successfully upload to the board.

The brief testing I did was all at 3v The original board took about 6mA running an "idle" sketch. Removing the LED dropped consumption by about  1.5mA, and removing the voltage regulator dropped another 0.3mA. Making the changes to the bootloader and fuses then resulted in an overall consumption of about 2.2mA. Now, this is still relatively high, but the "idle" sketch just runs in a tight loop without any power saving features.

3 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete