{"id":209,"date":"2014-04-01T13:10:31","date_gmt":"2014-04-01T11:10:31","guid":{"rendered":"http:\/\/heppg.de\/ikg\/wordpress\/?p=209"},"modified":"2017-02-23T08:11:20","modified_gmt":"2017-02-23T07:11:20","slug":"amel-atmega328-used-as-ad-converter","status":"publish","type":"post","link":"https:\/\/heppg.de\/ikg\/wordpress\/?p=209","title":{"rendered":"Atmel atmega328 used as AD-Converter for Raspberry Pi"},"content":{"rendered":"<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/?p=164\">[german<\/a>]<\/p>\n<p>see also: <a href=\"http:\/\/heppg.de\/ikg\/wordpress\/?p=532\">Arduino UNO, NANO connected to Scratch<\/a><\/p>\n<h1>Atmel 328-Prozessor used as AD-Converter<\/h1>\n<p>The microcontroller <a title=\"Datasheet\" href=\"http:\/\/www.atmel.com\/Images\/doc8161.pdf\">ATmega328P <\/a>in DIL-package can be used on a breadboard.<\/p>\n<p>This controller has many build-in functions, as ADConverter, timers SPI and more.<br \/>\nThe Arduino boards use this controller, making it very popular and many literature is available.<br \/>\nThe ADC has 10Bit resolution and in DIP package there are 5 channels available.<\/p>\n<p>Wiring to Raspberry Pi is quite simple. The firmware can be programmed directly from RPi, no programming device needed. The focus of this article is not the programming of the device. The needed firmware is attached to the samples.<br \/>\nCost of the devive are comparable to simple ADConvertes and are about 4\u20ac.<\/p>\n<p>Disadvantage is the more complex setup needed.<\/p>\n<p>On the <a href=\"http:\/\/heppg.de\/ikg\/wordpress\/?page_id=6\">download page<\/a>, I provide a firmware which can read two AD-channels and to control a LED. More functions are planned, as frequency measurement, infrared control or rotary encoders.<\/p>\n<p>The firmware of the controller can be programmed with the RPi. No extra flashing hardware is needed.<br \/>\nPrerequisite is that the controller has the internal 8MHz oscillator enabled. This is factory preset.<br \/>\nBasic setup of the controller is defined by &#8216;fuses&#8217;, especially the setup of the oscillator used. Without a working oscillator, programming is not possible.<br \/>\nIf you get an already flashed device, e.g. prepared to be used in an arduino board, these will have internal oscillator shut off. In this case, reading out the software will not work. Workaround is to activate GPIO#4 of RPi as a clock source and connect to the 328. See attachments for a short tutorial. Or to use a flashing hardware.<br \/>\nCaution: when you use a device from an arduino board it is likely that the bootloader is overwritten. There is no simple way back.<\/p>\n<p>parts list<\/p>\n<ul>\n<li>ATmega 328 P-PU (DIL-package)<\/li>\n<li>breadboard<\/li>\n<li>precision socket for the processor. DIL 28 pin, 0.3 wide. Insert the socket to the breadboard, and insert the controller into socket. The controller can be used directly, but with the socket insertion and removal is simpler.<\/li>\n<li>LED. The longer wire is the anode and is the &#8216;positive&#8217; side.<\/li>\n<li>resistor 1kOhm<\/li>\n<li>capacitor ceramik 100nF, min 10V<\/li>\n<li>trim potentiometer for test<\/li>\n<li>cables f-m, 7 or 8 pieces<\/li>\n<li>wires for breadboard.<\/li>\n<\/ul>\n<h2>Setup procedure<\/h2>\n<p>Basic steps are wiring, flashing the controller and activating scratchClient software.<\/p>\n<h3>electrical setup<\/h3>\n<p>Shut down RPi and disconnect power source.<\/p>\n<p>Place processor on breadboard and connect according to scheme.<\/p>\n<p>VCC 3.3V is taken from from Raspberry.<br \/>\nSPI-connections MISO, MOSI, SCK and SS at CS0 of RPI allow communication.<br \/>\nRESET pin 1 to GPIO24 is needed for flashing in addition to SPI.<br \/>\nLED with serial resistor 1kOhm is connected to PB1, Pin 15<a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2014\/04\/atmel_Steckplatine1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-248\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2014\/04\/atmel_Steckplatine1.png\" alt=\"atmel_Steckplatine\" width=\"2175\" height=\"1581\" \/><\/a>.<br \/>\nADC-inputs used are ADC0 and ADC1, Pin 23, 24.<\/p>\n<p><a href=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2014\/03\/sockel.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-180 alignleft\" src=\"http:\/\/heppg.de\/ikg\/wordpress\/wp-content\/uploads\/2014\/03\/sockel.jpg\" alt=\"Prozessor im Sockel\" width=\"321\" height=\"217\" \/><\/a><\/p>\n<p>This is how the controller sits in socket on breadboard.<\/p>\n<p>&nbsp;<\/p>\n<h3>Installing flash software on Raspberry Pi<\/h3>\n<p><span style=\"color: #000080;\"><span style=\"font-family: Courier New,monospace;\"><span style=\"font-size: xx-small;\">sudo apt-get update<br \/>\nsudo apt-get upgrade -y<br \/>\nsudo apt-get install python-dev pip<br \/>\nsudo pip install spidev intelhex<\/span><\/span><\/span><\/p>\n<p>Activate SPI driver. Use raspi-config , enable SPI.<\/p>\n<p>Copy the programming software to \/home\/pi .<\/p>\n<p><a title=\"Download software\" href=\"http:\/\/heppg.de\/ikg\/administration\/pi\/program_328\/download\/program_328.tar.gz\">Download software<\/a> and store in \/home\/pi and unpack &#8220;tar xfvz program_328.tar.gz&#8221;.<\/p>\n<p><span style=\"color: #000080;\"><span style=\"font-family: Courier New,monospace;\"><span style=\"font-size: xx-small;\">tar xzvf program_328.tar.gz<\/span><\/span><\/span><\/p>\n<h3>Validate hardware and software setup<\/h3>\n<p>The following steps validate correct cabling and whether flashing the controller is possible.<\/p>\n<h4>Read Fuses<\/h4>\n<p><span style=\"color: #000080;\"><span style=\"font-family: Courier New,monospace;\"><span style=\"font-size: xx-small;\">cd ~\/program_328<br \/>\nsudo python src\/program.py -rf<\/span><\/span><\/span><\/p>\n<p>The output should look similiar to<\/p>\n<pre><span style=\"font-family: Courier New,monospace;\"><span style=\"font-size: xx-small;\">PROGRAMMING_READ_CALIBRATION_BYTE b0 10110000\r\n PROGRAMMING_READ_EXTENDED_FUSE_BITS ff 11111111\r\n BODLEVEL0 1\r\n BODLEVEL1 1\r\n BODLEVEL2 1\r\n PROGRAMMING_READ_FUSE_BITS e2 11100010\r\n CKSEL0 0 ENABLED\r\n CKSEL1 1\r\n CKSEL2 0 ENABLED\r\n CKSEL3 0 ENABLED\r\n SUT0 0 ENABLED\r\n SUT1 1\r\n CKOUT 1\r\n CKDIV8 0\r\n PROGRAMMING_READ_FUSE_HIGH_BITS d9 11011001\r\n BOOTRST 1\r\n BOOTSZ0 0 ENABLED\r\n BOOTSZ1 0 ENABLED\r\n EESAVE 1\r\n WDTON 1\r\n SPIEN 0 ENABLED\r\n DWEN 1\r\n RSTDISBL 1\r\n PROGRAMMING_READ_LOCK_BITS ff 11111111<\/span><\/span><\/pre>\n<p>If there are errors (Device not in sync), then either cables are not correct connected or the processor has wrong fuses already programmed.<br \/>\nCheck connections. If error persists, then connect external clock.<\/p>\n<h4>Read out current code<\/h4>\n<p>Another step in validating the setup is to read current code. Should be empty on a brand new device, but is useful to check communication.<\/p>\n<p><span style=\"color: #000080;\"><span style=\"font-family: Courier New,monospace;\"><span style=\"font-size: xx-small;\">cd ~\/program_328<br \/>\nsudo python src\/program.py -r<\/span><\/span><\/span><\/p>\n<p>The output should look similiar to<\/p>\n<pre><span style=\"font-family: Courier New,monospace;\"><span style=\"font-size: xx-small;\">python src\/program.py -r\r\n ('read', 'out.hex')\r\n programming_readCode\r\n programming_enable\r\n ('PROGRAMMING_ENABLE', [172, 83, 0, 0])\r\n (0, [255, 255, 83, 0])\r\n programming_enable end\r\n programming_disable\r\n programming_readCode ende\r\n ok<\/span><\/span><\/pre>\n<h4>Flash testcode to controller<\/h4>\n<p>When everything went smooth so far, then it is time to load the first testcode into the controller. It dies nothing but let the LED blink.<\/p>\n<p><span style=\"color: #000080;\"><span style=\"font-family: Courier New,monospace;\"><span style=\"font-size: xx-small;\">cd ~\/program_328<br \/>\nsudo python src\/program.py -p 328\/steckbrett_328_blink.hex<\/span><\/span><\/span><\/p>\n<p>LED should blink slowly.<\/p>\n<h4>Program Fuses for oszillator<\/h4>\n<p>The controller runs with internal oscillator, but frequency is limited to 1MHz. By setting the correct fuses, the Controller runs with 8MHz.<\/p>\n<p>Please do not program the fuses, if there have been errors in steps before (which are not yet fixed) and the LED is blinking.<\/p>\n<p><span style=\"color: #000080;\"><span style=\"font-family: Courier New,monospace;\"><span style=\"font-size: xx-small;\">cd ~\/program_328<br \/>\nsudo python src\/program.py -wf<\/span><\/span><\/span><\/p>\n<p>Blinking should stop shortly, and restart quicker, 5 times a second.<br \/>\nOscillator setup is completed now.<\/p>\n<h3>Flash firmware to controller.<\/h3>\n<p>Final step is flashing the application firmware. It handles the AD-Conversions, LED-Settings and alike.<br \/>\n<span style=\"color: #000080;\"><span style=\"font-family: Courier New,monospace;\"><span style=\"font-size: xx-small;\">cd ~<\/span><\/span><\/span><span style=\"color: #000080;\"><span style=\"font-family: Courier New,monospace;\"><span style=\"font-size: xx-small;\">\/program_3<\/span><\/span><\/span><span style=\"color: #000080;\"><span style=\"font-family: Courier New,monospace;\"><span style=\"font-size: xx-small;\">28<br \/>\nsudo python src\/program.py -p 328\/steckbrett_328.hex<\/span><\/span><\/span><\/p>\n<p>After programming is complete, the LED should blink 8 times. This is part of the RESET routine.<\/p>\n<h3>Application &#8211; testcode<\/h3>\n<p>Blink the LED, long bright, short dark.<br \/>\n<span style=\"color: #000080;\"><span style=\"font-family: Courier New,monospace;\"><span style=\"font-size: xx-small;\">cd ~\/program_328<br \/>\nsudo python src\/test_blink.py<\/span><\/span><\/span><br \/>\nThe duty cycle is set by the python code in side Raspberry. This indicates that it is the reaspberry having control.<\/p>\n<p>Read firmware version.<br \/>\n<span style=\"color: #000080;\"><span style=\"font-family: Courier New,monospace;\"><span style=\"font-size: xx-small;\">cd ~\/program_328<br \/>\nsudo python src\/test_get_version.py<\/span><\/span><\/span><\/p>\n<p>Display should show &#8217;93 09&#8242;; second number is minor version and can be higher.<\/p>\n<p>Read ADC, Channel 0<br \/>\n<span style=\"color: #000080;\"><span style=\"font-family: Courier New,monospace;\"><span style=\"font-size: xx-small;\">cd ~\/program_328<br \/>\nsudo python src\/test_adc_0.py<\/span><\/span><\/span><\/p>\n<h2>Scratch-Connection<\/h2>\n<p>When tests for application software work, the next step is the connection to scratch.<\/p>\n<p><span style=\"color: #000080;\"><span style=\"font-family: Courier New,monospace;\"><span style=\"font-size: xx-small;\">cd ~\/scratchClient<br \/>\nsudo python src\/scratchClient.py -config config\/config_adc_atmel328.xml<br \/>\n<\/span><\/span><\/span><\/p>\n<p>Configuration can be adjusted in the config file. ADC-channels can be enabled \/ disabled, or reference voltage can be 3.3-V or the internal reference 1.1V.<br \/>\nThe internal reference voltage is useful when an tempperature sensor TMP036 is connected.<br \/>\nThe 3.3V-reference is perfect when a potentiometer is connected.<\/p>\n<p>The ADC values are sent with variable names &#8216;adc_0&#8217; und &#8216;adc_1&#8217; to scratch, range is 0..1023.<br \/>\nBroadcast commands from scratch to controller are &#8216;led_off&#8217;, &#8216;led_on&#8217;.<\/p>\n<h2>SPI-Communication between RPi and Controller<\/h2>\n<p>SPI is a synchronous protocol. The Controller as slave has no chance to indicate &#8216;slower please&#8217; to the master. Therefor, each command from RPi to controller needs to be answered in the time between receiving the first an second byte of a sequence. Otherwise, there will be garbage read back.<\/p>\n<p>This limits transfer speed to 240kHz with the 8MHz controller. With the 20MHz variant, this will be faster.<\/p>\n<h2>Appendix: GPIO#4 of RPi as clock-input for 328<\/h2>\n<p>Der GPIO#4-Pin can be configured to provide a MHz range clock signal for the controller. The program for this is in bin\/-folder. It is based on code from guzunty.org.<\/p>\n<p>Connect GPIO#4 with XCLK-Eingang of controller. Then start program bin\/gz_clock_1.92MHz.<\/p>\n<pre>cd bin\r\nchmod +x gz_clock_1.92MHz \r\nsudo .\/gz_clock_1.92MHz<\/pre>\n<p>While this program is running, execute read\/write of fuses in a second terminal window.<br \/>\nThen disconnect GPIO#4.<\/p>\n<pre>Update history:\r\n2014-04-01 Fixed connections of LED<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>[german] see also: Arduino UNO, NANO connected to Scratch Atmel 328-Prozessor used as AD-Converter The microcontroller ATmega328P in DIL-package can be used on a breadboard. This controller has many build-in functions, as ADConverter, timers SPI and more. The Arduino boards use this controller, making it very popular and many literature is available. The ADC has [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,3],"tags":[],"class_list":["post-209","post","type-post","status-publish","format-standard","hentry","category-analog-digital-wandler","category-scratchclient"],"_links":{"self":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=209"}],"version-history":[{"count":14,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions"}],"predecessor-version":[{"id":908,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions\/908"}],"wp:attachment":[{"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/heppg.de\/ikg\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}