Using ATmega16 with Arduino IDE

ATmega16 ATmega32 Arduino Pinout

A few days ago I searched in my microcontrollers box (one of them 🙂 ) and I came across some ATmega16. I almost forgot I had these microcontrollers there. In recent years I became more involved in using different ATmega microcontrollers with Arduino and so I decided to make it work without using a ready-made solution. Already done it this with ATmega1284.(see here)

The main task when we want to add a “new” Atmel microcontroller in Ardiuno IDE is to write a new file pins_arduino.h and make some additions in boards.txt. I have not forgot bootloader, but as long as I never using it (I always use USBASP for my custom Arduino projects), I’ve not bother with it. (and have the advantage of using all 16k flash memory).

One more thing: we have to set fuse bits according with qur needs. A good tutorial with online fuse calculator is at Limor site: http://www.ladyada.net
“Fuses are an extremely important part programming a chip, but are rarely explained thoroughly. You only need to set them once, but if you don’t do it right, it’s a disaster!”http://www.ladyada.net/learn/avr/fuses.html

Another good tutorial about ATmega fuse bits here: https://embedderslife.wordpress.com/2012/08/20/fuse-bits-arent-that-scary/.

boards.txt

Locate file boards.txt in arduino folder …[arduino folder]\hardware\arduino\avr\boards.txt
Append following information to boards.txt

pins_arduino.h

Make a folder named Atmega16 in …[arduino folder]\hardware\arduino\avr\variants
Copy file pins_arduino.h from the zip Atmega16_Arduino.zip into folder previously created.
Now it is ready.

First tests

ATmega DIP40 small development board

ATmega DIP40 small development board with ATmega16 at 16MHz

We can run a few classical examples from Arduino. I kept standard connection of LED to Arduino digital pin 13 (=PD5, pin 19 of IC ATmega16/DIP40)

1. Blink example:
Turns on an LED on for one second, then off for one second, repeatedly.
The circuit: LED attached from digital pin 13 (PD5) to ground.

2. Fading example:
This example shows how to fade an LED using the analogWrite() function.
The circuit: LED attached from digital pin 13 (PD5) to ground.

3. Analog read example:
… in progress…

Related posts:

http://openhardware.ro/atmega-40-pin-minimal-board/

http://openhardware.ro/using-atmega1284-arduino-ide/

http://openhardware.ro/using-atmega32-arduino-ide/

UPDATE:

For Serial library to work properly must be made following changes to the file HardwareSerial.cpp
In …\arduino-1.5.8\hardware\arduino\avr\cores\arduino\HardwareSerial.cpp

we will replace:

with:
Share
Updated: April 9, 2015 — 6:13 pm

46 Comments

Add a Comment
  1. Arduino: 1.5.6-r2 (Windows 7), Placa:”Atmega16 (internal 8MHz clock)”

    In file included from C:\Arduino\hardware\arduino\avr\cores\arduino\HardwareSerial.cpp:32:
    C:\Arduino\hardware\arduino\avr\cores\arduino\/HardwareSerial_private.h: In member function ‘void HardwareSerial::_rx_complete_irq()’:
    C:\Arduino\hardware\arduino\avr\cores\arduino\/HardwareSerial_private.h:98: error: ‘UPE’ was not declared in this scope

    This report would have more information with
    “Show verbose output during compilation”
    enabled in File > Preferences.

    I can not compile, helpme

    1. I check using serial from example DigitalReadSerial. Arduino 1.5.8 , Windows 7, Board: Atmega16 – External 8Mhz.
      It worked smoothly.

      I guess that HardwareSerial_private.h is broken … try reinstal Arduino IDE and make modifications again .. or instal 1.5.8 and make modifications again.

      In 1.5.8
      void HardwareSerial::_rx_complete_irq()
      is at line 101…
      may miss some instructions from begining of HardwareSerial_private.h

  2. why only 3 analog out(PWM) pin? can I use more pin for AnalogWrite?

    1. AnalogWrite uses timers for PWM output, ATmega16 and ATmega32 has 4 PWM channels (according with datasheet).
      Also, ATMega16/32 has 3 timers Timer0(8bit), Timer1(16bit), Timer2(8bit). For PWM output, pins PB3 PD4,PD5,PD7 are connected internally to timers in this way:
      PB3 to Timer0 (we can’t use this because Timer0 is already used by arduino core)
      PD4 to Timer1 (Timer1 has 16 bits, so half of it it’s used like 8bit timer)
      PD5 to Timer1 (Timer1 has 16 bits, so other half of it it’s used like 8bit timer)
      PD7 to Timer2 (Timer2 has 8 bits, it can otput only one PWM channel)

      Therefore we can use 3 of maximum 4 chanels available.

  3. If cange what you suggest in HardwareSerial.cpp, takes the error:
    config is not declared in this scope..
    so I did not do the chage,
    trying the Blink example:
    Programmer “ArduinoISP”
    remains at uploading stage
    writes message:
    avrdude error:could not find USBtiny device (ox2341/ox49)
    Please help!!

    1. From the error messages I understand that you use ArduinoISP as programmer and/or USBtiny…it is not clear…
      Also I use Arduino IDE 1.5.8 for this article… (for 1.6.4 I have another solution)…

      It should work without HardwareSerial.cpp modifications. (these changes are required if you want to use serial port of microcontroller)

      “config is not declared in this scope” tell me there are more errors in arduino core files… try reinstal Arduino IDE and make modifications again…

  4. It gives a compilation error. I am using USBasp programmer.
    I installed the drivers and WINavr. please help asap

    the error is:
    Property line ‘?##############################################################’ in file D:\Arduino\hardware\arduino\avr\boards.txt is invalid

    Error compiling.
    Invalid library found in C:\Users\Shashank\Documents\Arduino\libraries\sketch_nov08a: C:\Users\Shashank\Documents\Arduino\libraries\sketch_nov08a
    Invalid library found in C:\Users\Shashank\Documents\Arduino\libraries\sketch_nov09a: C:\Users\Shashank\Documents\Arduino\libraries\sketch_nov09a
    Invalid library found in C:\Users\Shashank\Documents\Arduino\libraries\sketch_nov08a: C:\Users\Shashank\Documents\Arduino\libraries\sketch_nov08a
    Invalid library found in C:\Users\Shashank\Documents\Arduino\libraries\sketch_nov09a: C:\Users\Shashank\Documents\Arduino\libraries\sketch_nov09a

    1. It seems that error is in file …\avr\boards.txt:

      ?##############################################################

      the “?” character is incorrect … just delete it

    2. What version of Arduino IDE you are using?
      I tried to reproduce the error with Arduino IDE 1.5.8 and default sketch was compiled ok.

      I guess that the directory structure of the application is altered.

      1. hi silvius
        i am running an atmega 32 internal 8Mhx and 1.6.6 arduino ide
        no way i can get the correct baaud rate!!
        i can only set the speed at 4800 and read at 600 !

        when i try with bascom i have no problems
        What can i do?

        i did add the lines
        #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega32__) || defined(__AVR_ATmega16__)
        config |= 0x80; // select UCSRC register (shared with UBRRH)
        #endif
        it looks like the baud rate is non calculated well
        thank’ for any help

        1. These files are tested with arduino 1.5.8. for > 1.6.4 try this:

          http://openhardware.ro/atmega-dip40-in-arduino-ide-1-6-4/

          1. hi silvius

            i have also problems with serial communication. I used atmega32 and atmega16 at 16MHz but i can’t see noting on serial monitor (i am running a simple hello world code).The Tx led of my serial to usb convertor indicate that Tx work bat nothin on serial monitor.
            can you help me please?

            1. Most frequently it’s about FUSE bits or USB to Serial Converter.
              Fuse bits must be set properly for 16MHz.

              You can try this:
              Low fuse :DE
              High fuse: D9

              I assume that you have already made changes in file
              …cores\arduino\HardwareSerial.cpp (it is mentioned in the end of the article)

  5. Hi, this tutorial works! Thanks very much.
    But still I have problem – my LED is blinking, but the interval (of blinking and not blinking) is approx. 16 seconds! (16 16 seconds on, 16 seconds off). I am using Atmega16, USBasp (with no external clock source) and Arduino IDE. Any reply?

    1. 16 seconds instead of 1 second tell me that MCU is configured (and work) at 1MHz (default speed) instead of 16 MHz.

      there are two solutions:

      1.You can put a 16MHz quartz and some ~18-22pF capacitors [b]and set fuse bits[/b] for that
      (see also comments above)

      2. You can define another board at 1MHz (in boards.txt) and use without external components… (max speed is 8MHz).

      It’s your choice…

      1. Oh thank you, ATmega16 was really set to use factory default 1Mhz. I bought crystall and now its working correctly.

  6. I am trying to use SoftwareSerial library for ATmega32-16MHz board, but receiving the following errors:
    C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial\src\SoftwareSerial.cpp: In member function ‘void SoftwareSerial::begin(long int)’:
    C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial\src\SoftwareSerial.cpp:319:36: error: ‘digitalPinToPCICR’ was not declared in this scope
    if (digitalPinToPCICR(_receivePin)) {
    ^
    In file included from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\io.h:99:0,
    from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\interrupt.h:38,
    from C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial\src\SoftwareSerial.cpp:41:
    C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial\src\SoftwareSerial.cpp:360:76: error: ‘digitalPinToPCICRbit’ was not declared in this scope
    *digitalPinToPCICR(_receivePin) |= _BV(digitalPinToPCICRbit(_receivePin));
    ^
    C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial\src\SoftwareSerial.cpp:363:51: error: ‘digitalPinToPCMSK’ was not declared in this scope
    _pcint_maskreg = digitalPinToPCMSK(_receivePin);
    ^
    In file included from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\io.h:99:0,
    from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\interrupt.h:38,
    from C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial\src\SoftwareSerial.cpp:41:
    C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial\src\SoftwareSerial.cpp:364:60: error: ‘digitalPinToPCMSKbit’ was not declared in this scope
    _pcint_maskvalue = _BV(digitalPinToPCMSKbit(_receivePin));
    ^
    exit status 1
    Error compiling for board ATmega32-16MHz.

    1. SoftwareSerial library use an interrupt that ATMEGA16 not have (nor ATMEGA32 and ATMEGA8): PCINT.

      So you can’t use newer versions of SoftwareSerial with these MCUs. Sorry.

      But there are much older versions of SoftwareSerial… those libraries might work with these MCUs.

  7. Can you share those older version library of softwareserial to work with ATMEGA16 MCU

    1. I do not have that library separately. Right now I will download some older versions of the Arduino IDE and I will extract from there.
      The trick is to select Tools -> Board: “Arduino NG or older” and Processor: “Atmega8” and try to compile.
      If it works for ATmega8, then it will works for ATMega16/32 (of course still need to install these processors/boards before).

      So in brief: To to verify that a library is compatible (or can be made compatible) with ATMega16/32, we can try to compile for oldest(first) Arduino board (with Atmega8 MCU).
      After that we can edit library and add support for Atmega 16/32 if is missing. Most often involve adding “Atmega16” “Atmega32” string to some macros…

    2. Now it’s done: OldSoftwareSerial.zip
      This is version from Arduino 0.23. It not tested in real hardware, only for compiling with ATmega16/32 boards (and ATmega8)
      This is an old piece of software… and does not have all the features that the new version has (because missing features of newer ATmega).
      It is not compatible with default SoftwareSerial library examples. You must study OldSoftwareSerial.h before use it.
      …from my point of view it’s not worth to go further, I expect to be very poor performance… Interestingly experiment. But only this.
      If I really need more serial links, I would use a microcontroller that even have PCINT available. ATmega 328 /644 /1284 available in DIP package.
      And 644 / 1284 already have two hardware serials available

  8. Hi,

    Port C0-C7 Not Working!! Please give Solution.

    1. you must turn off JTAG interface…
      temporary solution:
      put this in setup section:

      for more about how to deal with JTAG:
      http://www.engineersgarage.com/embedded/avr-microcontroller-projects/disable-jtag-port

  9. Hi, i want to use atmega16a as modbus slave, my code works for atmega 2560 but not for atmega16a, i use rx and tx (PD0-1) and the same circuit but it wont work, i didn’t see anything about RX-TX inside pins_arduino.h file, i should type something inside it?

    1. For serial library to work properly must follow update part of this article (the last paragraph):

      in short you must make some changes to the file:
      …\arduino-xxxx\hardware\arduino\avr\cores\arduino\HardwareSerial.cpp

      If serial(USART) will work properly I think modbus code will work properly.

      1. Thx for reminding me 🙂 I’ll look at it as soon as possible, thanks 🙂

      2. Hi again, it’s not communicating at 9600 baudrate, it sometimes communicates at 300 baudrate sometimes 600 baudrate and also it stops communicating after a few minutes later, did you see something like that before?

        1. I saw such manifestation only before before making changes in HardwareSerial.cpp. You’d better make sure you have correctly identified which HardwareSerial.cpp is used by Arduino IDE… it is possible to have more copies and only one is used in compilation.

          Try to test a serial (RS232) connection between your project and your PC… after all work ok
          you can test with modbus link…

          Also if you use newer Arduino IDE ( >1.6.xx) try this: http://openhardware.ro/atmega-dip40-in-arduino-ide-1-6-4/

          but not forget to modify HardwareSerial.cpp after install… 🙂
          good luck!

          1. I got only one HardwareSerial.cpp inside files, and others are HardwareSerial0.cpp – HardwareSerial1.cpp – HardwareSerial2.cpp – HardwareSerial3.cpp, should i do something inside them? i got 1.8 version and i already used the link u gave me and i downloaded all 6 microprocessors the site has, still i got that problem, do you have any modbus code you know it’s working? but i am pretty sure that it works for mega 2560

            1. I guess that your modbus code it’s working but I suggest that is better to “isolate” problem. It seems that problem is initialisation of USART(serial), so it is better to experiment with this interface before use modubus code…
              I think is easy to iterate through experiments in that way… If simple serial example working ok, then problem is solved.

              It is no need to modify HardwareSerial0.cpp … HardwareSerial3.cpp

              Also I guess that you use some windows… I do not use Windows for a long time (I use Linux ), but I remember that HardwareSerial.cpp is in two locations at least.
              One copy is in Arduino folder and another copy (most important) used in compilation is hidden in user profile folder.
              Please check that last location and modify that file.

              You can verify if you have found correct file, you can deliberate put some wrong code in that file, and test if that generate an error.

              And most important: make this modification (in HardwareSerial.cpp)after you install OR update arduino core…

              If you make an update the modifications are lost… so you must make modifications again.

              Also I make myself some test (few months ago) with modbus / RS485 and Atmega32 boards and Arduino IDE and worked smoothly… so keep trying … I hope you will find soon where the problem is…

              1. Hi again Silvius, I still have problems, I need to ask you a few questions, Can u please contact me via my mail adress. Thank you 🙂

  10. Hi Silvius i am planning to work on vehicle tracking project for which i need two uarts
    i want to use atmega32 which has one uart that means i will need one software uart.will software serial library of arduino ide work for atmega32?

    1. sorry… software serial library of arduino ide not work with atmega32/16/8. You have two options:
      1) move to atmega328/DIP package (Arduino UNO 100% compatible) – software serial will work as expected for UNO

      2) move to atmega644/1284/1284p – software serial will work and you have two hardware USART;
      for 1284 and 1284p will find info on this site… there are other solutions on other sites…
      I don’t recall right now… for 644 I have not defined boards… because I “jump” from Atmega32 right to Atmega1284… 🙂

  11. Hi Silvi, im using atmega 16 to driver a motor DC usung pwm,but output pwm from pin PD5 is same,There is no different of OCR value,how to solve that?

  12. Hi Silvi, im using atmega 16 to driver a motor DC using pwm,but output pwm from pin PD5 is same,There is no different of OCR value,how to solve that?

    1. Use this code as a starting point:

      int ledPin = 13; // LED connected to digital pin PD5
      // use 13 for PD5 (not PD5)
      // and 12 for PD4

      void setup() {
      // nothing happens in setup
      }

      void loop() {
      // fade in from min to max in increments of 5 points:
      for (int fadeValue = 0 ; fadeValue < = 255; fadeValue += 5) { // sets the value (range from 0 to 255): analogWrite(ledPin, fadeValue); // wait for 30 milliseconds to see the dimming effect delay(30); } // fade out from max to min in increments of 5 points: for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
      // sets the value (range from 0 to 255):
      analogWrite(ledPin, fadeValue);
      // wait for 30 milliseconds to see the dimming effect
      delay(30);
      }
      }

      1. Sorry Silvi, i try see their output of pwm without osyloskop,but it doesn’t output square wave form and the led doesnt fading but just on and off

        1. I can assure you that working. I just now tested FADE example from standard arduino examples; With atmega16a and ledPin = 13 for PD5 … The problem is elsewhere…

          Maybe you should revise your schematic… ? How you drive DC motor? with the transistor? is that part 100% functional… maybe burn PD5 port when experimenting?

          maybe other part of the code interfere with analogWrite from Arduino?

  13. Hello
    I’m having trouble using the serial port
    11059200 MHz crystal information is not sent correctly
    where is the problem from?
    All steps are done correctly

    1. yes… of course it is not working correctly… 🙂
      the boards are defined for 8 and 16 MHZ…
      It is not complicated to define a new “board”:

      add in boards.txt something like this:


      ##############################################################

      ATmega16-11.name=ATmega16-11MHz
      ATmega16-11.upload.tool=avrdude
      ATmega16-11.build.mcu=atmega16
      ATmega16-11.build.f_cpu=11059200L
      ATmega16-11.build.board=AVR_ATMEGA16-11
      ATmega16-11.build.core=arduino:arduino
      ATmega16-11.build.variant=ATmega16
      ATmega16-11.upload.maximum_size=16000

      ##############################################################

      Atmega16-11 is the name, so it is no necessary to put in name the exact value.
      the most important line is this
      ATmega16-11.build.f_cpu=11059200L

      a new board should appear in your boards menu.

  14. Thanks for your answer
    Of course, I had already done
    But still does not work properly

    1. hm… If you already done defining new board… and made changes in HardwareSerial.cpp… it should work properly…

      It is one thing that maybe is wrong…
      It is possible that HardwareSerial.cpp that you have identified not to be the one used by Arduino IDE…I did so when I switched to 1.6…
      it is possible that file is hidden somewhere in your personal folder or home folder…
      depends on the operating system and its version… I use linux and files are in the
      /Home/.arduino15/packages/

  15. Windows 64-bit
    Arduino 1.8.1
    AT mega16
    I changed HardwareSerial.cpp
    || defined (__ AVR_ATmega16__)
    and add in boards.txt your code
    Are you sure that aruino work with this crystal
    I am using the ISP programer
    I set Fuse bits to external crystal
    Other features will work correctly
    But is not properly send and receive serial data

    1. I have not used this quartz so far, otherwise I would have already defined a board for it. The next days I will try to find such a crystal and I will try to see what it is about.

    2. I tested with 11.059200 MHz quartz, 9600-115200 baud. Everything works ok.

      I’m pretty sure the problem is somewhere else.
      Maybe it is not right HardwareSerial.cpp…

      Maybe USB to Serial converter not working properly.
      I had big problems with those adapters in windows, especially those with (pirated)PROLIFIC chip.
      Good luck!

      1. Thanks for the answers and your attention

Leave a Reply

Your email address will not be published. Required fields are marked *

OpenHardware.Ro © 2017 Frontier Theme