{"id":351,"date":"2018-02-25T13:15:41","date_gmt":"2018-02-25T13:15:41","guid":{"rendered":"http:\/\/www.adrian-smith31.co.uk\/blog\/?p=351"},"modified":"2020-11-09T17:10:57","modified_gmt":"2020-11-09T17:10:57","slug":"what-to-do-with-a-blown-faulty-atmega328-dont-throw-it-out-it-may-still-be-useful","status":"publish","type":"post","link":"https:\/\/www.adrian-smith31.co.uk\/blog\/2018\/02\/what-to-do-with-a-blown-faulty-atmega328-dont-throw-it-out-it-may-still-be-useful\/","title":{"rendered":"What to do with a blown \/ faulty ATmega328 &#8211; Don&#8217;t throw it out&#8230; It may still be useful."},"content":{"rendered":"<p>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&#8217;t need many ports.<\/p>\n<p>The chip&#8217;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 &#8216;partially high&#8217; when they shouldn&#8217;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&#8217;s pins resulted in some other pins&#8217;s output voltages dropping significantly. The display had adequate resistors and I even tried connecting it via a ULN2803 IC to no avail.<\/p>\n<p><!--more--><\/p>\n<p>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 &#8216;kits&#8217; 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.<\/p>\n<p>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&#8217;t know. I doubt the Chinese can fake an entire microcontroller so I&#8217;m thinking the PCB it was installed on got shorted on some stray cut component leads on the bench.<\/p>\n<p><strong>So what did I use it for?<\/strong><\/p>\n<p>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.<a href=\"http:\/\/www.adrian-smith31.co.uk\/blog\/2018\/01\/arduino-gas-detector\/\"> I wrote a basic gas detector<\/a> 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 &#8220;The difference between screwing around and science is writing it down&#8221; so I did. Anyway, due to the cost of the three sensors I didn&#8217;t bother with building it into a finished project. I couldn&#8217;t justify the cost of the components for project that hasn&#8217;t got a serious use. I still bought the cheapest sensor though, the MQ2.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>***Update Jan 2019***<\/p>\n<p>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 <a href=\"https:\/\/circuitdigest.com\/microcontroller-projects\/arduino-smoke-detector-on-pcb-using-mq2-gas-sensor\" target=\"_blank\" rel=\"noopener noreferrer\">this project<\/a> on Circuit Digest which shows the formula required. I did try to work it out myself but never managed to do so.<\/p>\n<figure id=\"attachment_366\" aria-describedby=\"caption-attachment-366\" style=\"width: 205px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/www.adrian-smith31.co.uk\/blog\/wp-content\/uploads\/2018\/03\/Gas_monitor.jpg\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-366 size-medium\" title=\"Gas \/ smoke detector for air quality monitoring or detecting leaks\" src=\"http:\/\/www.adrian-smith31.co.uk\/blog\/wp-content\/uploads\/2018\/03\/Gas_monitor-205x300.jpg\" alt=\"Gas \/ smoke detector for air quality monitoring or detecting leaks\" width=\"205\" height=\"300\" srcset=\"https:\/\/www.adrian-smith31.co.uk\/blog\/wp-content\/uploads\/2018\/03\/Gas_monitor-205x300.jpg 205w, https:\/\/www.adrian-smith31.co.uk\/blog\/wp-content\/uploads\/2018\/03\/Gas_monitor.jpg 539w\" sizes=\"auto, (max-width: 205px) 100vw, 205px\" \/><\/a><figcaption id=\"caption-attachment-366\" class=\"wp-caption-text\">Completed detector (OK I replaced the micro with a working one and added another digit)<\/figcaption><\/figure>\n<p><strong>Download and a word on display connection.<\/strong><\/p>\n<p>The sketch for the Arduino can be <a href=\"http:\/\/www.adrian-smith31.co.uk\/blog\/wp-content\/uploads\/2019\/01\/Gas_Detector_3_digit_V3.zip\">downloaded here<\/a>. 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.<\/p>\n<p>I modified sevenseg.cpp as such:-<\/p>\n<p><span style=\"color: #993300;\">void SevenSeg::setCommonAnode(){<\/span><br \/>\n<span style=\"color: #993300;\"> _digOn=HIGH;<\/span><br \/>\n<span style=\"color: #993300;\"> _digOff=LOW;<\/span><br \/>\n<span style=\"color: #993300;\"> _segOn=LOW;<\/span><br \/>\n<span style=\"color: #993300;\"> _segOff=HIGH;<\/span><\/p>\n<p>to<\/p>\n<p><span style=\"color: #993300;\">void SevenSeg::setCommonAnode(){<\/span><br \/>\n<span style=\"color: #993300;\"> _digOn=LOW;<\/span><br \/>\n<span style=\"color: #993300;\"> _digOff=HIGH;<\/span><br \/>\n<span style=\"color: #993300;\"> _segOn=LOW;<\/span><br \/>\n<span style=\"color: #993300;\"> _segOff=HIGH;<\/span><\/p>\n<p>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 <em>unmodified\u00a0<\/em>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.<\/p>\n<p><strong>LED resistor calculation for a multiplexed display<\/strong><\/p>\n<p>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 <em>DC current<\/em> 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 <em>average<\/em> 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&#8217;s beyond the scope of this article.<\/p>\n<p>To overcome the reduction in brightness reduce the series resistor&#8217;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&#8217;t recommend doing this. I suspect it hasn&#8217;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.<\/p>\n<p>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 <a href=\"http:\/\/www.adrian-smith31.co.uk\/blog\/2018\/01\/arduino-gas-detector\/\">check out my other program<\/a> which can give (with some modification) a breakdown of different gas concentrations in farts. See which foods produce what type of gas. That&#8217;s science.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;t need many ports. The chip&#8217;s pins all functioned OK but it had [&hellip;]<\/p>\n","protected":false},"author":179,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"What to do with a blown \/ faulty ATmega328 - Don't throw it out... It may still be useful. - Adrian's electronics blog","description":"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 c"},"footnotes":""},"categories":[1,4],"tags":[],"class_list":["post-351","post","type-post","status-publish","format-standard","hentry","category-uncategorized","category-microcontrollers"],"_links":{"self":[{"href":"https:\/\/www.adrian-smith31.co.uk\/blog\/wp-json\/wp\/v2\/posts\/351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.adrian-smith31.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.adrian-smith31.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.adrian-smith31.co.uk\/blog\/wp-json\/wp\/v2\/users\/179"}],"replies":[{"embeddable":true,"href":"https:\/\/www.adrian-smith31.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=351"}],"version-history":[{"count":21,"href":"https:\/\/www.adrian-smith31.co.uk\/blog\/wp-json\/wp\/v2\/posts\/351\/revisions"}],"predecessor-version":[{"id":667,"href":"https:\/\/www.adrian-smith31.co.uk\/blog\/wp-json\/wp\/v2\/posts\/351\/revisions\/667"}],"wp:attachment":[{"href":"https:\/\/www.adrian-smith31.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.adrian-smith31.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.adrian-smith31.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}