What to do with a blown / faulty ATmega328 – Don’t throw it out… It may still be useful.

In one of my past arduino projects I accidentally damaged a Arduino Mega328 MCU (or it may have been faulty to begin with) but I decided to keep hold of it in case I needed it for a very basic project that didn’t need many ports.

The chip’s pins all functioned OK but it had a strange problem; when the serial port was actively sending data or any other pins on port D were being used, some of the other pins were going ‘partially high’ when they shouldn’t. I noticed this when I connected a 7 segment display to the board as several segments flickered or lit dimly when adjacent pins were high. Any attempts at multiplexing or any other high speed manipulation of port D’s pins resulted in some other pins’s output voltages dropping significantly. The display had adequate resistors and I even tried connecting it via a ULN2803 IC to no avail.

I was going round in circles with this one thinking it was my dodgy programming. Substituting the chip for another one cured the problem. The only thing I can think of is the chip developed an internal short or low resistance between several pins as data on one would affect others. Very strange. The chip came from ebay as part of one of those ‘kits’ where you get the chip pre-programmed with an Arduino bootloader and a crystal, 28 pin IC socket plus a couple of capacitors. I bought 5 but the others seemingly had no issues but there again I only used the ADC and i2C ports which work OK on this chip.

Are they Atmel rejects that should have been destroyed and found their way to ebay sellers? Fakes? Or was it just broken by my careless handling? I don’t know. I doubt the Chinese can fake an entire microcontroller so I’m thinking the PCB it was installed on got shorted on some stray cut component leads on the bench.

So what did I use it for?

Anyway rather than throw it out I found that it can drive a dual digit 7 segment display without issues (on certain pins) and earlier this year I was playing around with some gas sensors namely the MQ-2. I wrote a basic gas detector that could detect 3 types of gases mainly for analyzing farts to see what gases they contained. For science right? As they once said on Mythbusters “The difference between screwing around and science is writing it down” so I did. Anyway, due to the cost of the three sensors I didn’t bother with building it into a finished project. I couldn’t justify the cost of the components for project that hasn’t got a serious use. I still bought the cheapest sensor though, the MQ2.

This sensor can detect explosive gases including methane and hydrogen which are both present in farts. I wrote another short program which shows the analog value on a 7 segment display. As this sensor gives out about 0.1-0.3V in clean air I decided that a dual display with a reading of 0-99 would suffice. There is also the possibility of dividing the value by 10 to give a scale of 0-999 instead for high gas concentrations.

In testing however, I found that the average fart does not give readings above 99 anyway. Further testing shows the sensor can detect alcahol, smoke and natural gas so this could be purposed as a gas leak monitor for your home or a air quality monitor.

***Update Jan 2019***

I modified this detector so it shows the approximate ppm value of the gas the sensor is detecting rather than the original version which just showed the analog value on the display. This is is a more useful tool and displays meaningful results. It works by calculating the parts per million of the detected gas by using a formula to calculate it from the sensors response curve. Readings are taken and compared to see if the gas level has changed and calculate its approximate value. The program is adapted from this project on Circuit Digest which shows the formula required. I did try to work it out myself but never managed to do so.

Gas / smoke detector for air quality monitoring or detecting leaks
Completed detector (OK I replaced the micro with a working one and added another digit)

Download and a word on display connection.

The sketch for the Arduino can be downloaded here. I used common anode displays and connected the display via 220 ohm resistors on the segment pins and PNP transistors on the digit pins. The library is designed for direct connection to the LED display so you will need to modify sevenseg.cpp if using transistors to drive the digits. This is highly recommended as if using a direct connection the total current if displaying 8 would be more than the 40mA the Mega328 can source which will damage the chip. You can directly connect the digit pins but you will have to use 470 ohm resistors instead of 220 ohm and a high efficiency low current display that is bright enough at only 2 or 3mA of segment current. Another option would be to reduce the duty cycle of the display.

I modified sevenseg.cpp as such:-

void SevenSeg::setCommonAnode(){
_digOn=HIGH;
_digOff=LOW;
_segOn=LOW;
_segOff=HIGH;

to

void SevenSeg::setCommonAnode(){
_digOn=LOW;
_digOff=HIGH;
_segOn=LOW;
_segOff=HIGH;

This is because PNP transistors invert the logic i.e logic low turns the transistor on and a logic high turns it off. Note that the download sketch has this section of code unmodified so if using LED displays directly connected (with 470 ohm resistors on the segment pins) use the sketch as is. If you wish to use PNP transistors you will have to do the modifications as above.

LED resistor calculation for a multiplexed display

The way multiplexing 7 segment displays works is a little complicated. For selecting a current limiting resistor for an LED you would normally use one based on the DC current through the LED. So 220 ohms with an LED of 2V forward voltage it would be 3V/220 ohms = 13.6mA DC current. Multiplexing turns a entire digit off then the next etc so with a 3 digit display only one digit is lit for 33% of the time which will reduce the average current through each segment to 4.5mA as in my example above. The displays I used were bright enough with only 4.5mA through them. With even larger number of digits it tends to get even more complex and the use of driver chips such as the ULN2803A / UDN2982 would be required to be able to supply the current needed to get a bright display. In theory a 10 digit display would work with 22 ohm resistors on the segment pins but definitely not recommended to connect them to your Arduino. Above 4 digits you are into driver chip territory. But that’s beyond the scope of this article.

To overcome the reduction in brightness reduce the series resistor’s value by 66% to give you the 13.6mA per segment therefore increasing brightness. PNP transistors would definitely be required in this case as each digit pin would be sourcing 95mA when displaying 8. Far in excess of the 20mA recommended and the 40mA absolute maximum. This would destroy your arduino. In my example with 220 ohms the maximum current per digit pin would be 31.5mA; still too much hence use of PNP transistors. However I have been using a 2 digit display without transistors for 2 years continuously but I wouldn’t recommend doing this. I suspect it hasn’t blown up the arduino because each digit pin is only on for 50% of the time. As I mentioned above a workaround is double the series resistor value (nearest value 470 ohms) and use an LED display that is bright enough at 2mA current. The instructions that come with the sevenseg library explain this in more detail; you really should check it out to avoid damaging anything.

Feel free to check it out if you are interested in finding which foods produce the most explosive farts (literally) or want a gas leak monitor to sit next to your boiler / gas stove etc. If you want to spend the cash on more sensors you can check out my other program which can give (with some modification) a breakdown of different gas concentrations in farts. See which foods produce what type of gas. That’s science.

3 Replies to “What to do with a blown / faulty ATmega328 – Don’t throw it out… It may still be useful.”

  1. A quick update on this. I’ve been busy building another gas detector using an MQ135 which I found to drift considerably. I guess the MQ2 will be the same so if this device is left powered for a long time the no gas present analog voltage will change. A new sensor should be ‘burned in’ for at least 24 hours but if it has been in storage for a long time this period can be longer.

    My MQ135 gave an analog value of 10 when first used raising to 15 after 24 hours. After being powered on continuously for 2 weeks this value had risen to 52. Leaving the sensor off for a week made the no gas analog value to be 33 but this rose back to 52 after a day.

    Bearing this in mind be aware you may need to adjust the analog values within the first few weeks if powered all the time. For occasional use the value given after the initial 24 hour burn in period should be fine.

    • Here’s a tip for these kinds of “drifting zero” cases: have the zero point be calculated in real time, via a low-pass filter with an extremely long time constant (eg. a few hours in this case).
      Remember to always use a “sanity check” limit to enforce a sane range of “zero” values when doing that.

      Of course this method only works with sensors like these gas detectors, where the normal reading is either zero, or very close to zero.
      With this method, it will still react to a rise in measured value if it’s at least a few times faster than the filter time constant – as well to a very slow buildup, as long as a suitable sanity limit exists (as noted above).

      Then at power on, either use the last known good value (stored in EEPROM; maybe not the best idea in this case though), or just assume that the power on value is equal to the zero value (ideally only after the sanity check, to verify it’s in the plausible range for a zero reading).

Comments are closed.