Hauke’s Projects

Playin' around with Electronics and Computers

Using PB6/7 of ATmega328P with Arduino IDE

The Goal

For a small project I used the ATmega328P MCU – and then the small project somewhat exploded and I needed more and more I/O-Pins. Suddenly all but the PB6 and PB7 pins were in use, and I needed exactly two more…

The Arduino IDE did not offer pin numbers for these pins, since they are used for the crystal oscillator on Arduino. My project however did not rely on ultra precise timing, so the internal oscillator was more than enough, leaving the two pins open for other use, but how to address them? I guess with Atmel Studio this would be rather easy, but I started in Arduino IDE and did not want to switch horses…  Browsing the net did bring up many hints (e.g. this and this), but no actual solution that worked in my case. Here is what I finally figured out – which works… kindof. And which is obsolete, because there is already a working…

Solution

Use MiniCore – does it all.

All I wrote below is outdated. DrAzzy gave me the hint to MiniCore in the Arduino forum – thanks again!

Obsolete: My own solution

just left it for completeness… and because it hurts to delete it 🙂

The following steps are necessary to achieve the goal:

  • Install board definitions for barebone ATmega328 et al.
  • Create dedicated pins_arduino.h definitions including the two pins
  • Extend the board definitions to use the modified pins_arduino.h

In detail:

Using ATmega328 and its Cousins with Ardiuno first place

Easy. Use the board definitions from carlosefr (Thanks for this!) – just follow the instructions on the linked github site.

Modifying pins_arduino.h

The actual pin assignments are stored in the file pins_arduino.h which can be found in the subfolders of C:\Program Files (x86)\Arduino\hardware\arduino\avr\variants or – this happened after an update of the libraries and boards for me – of %LOCALAPPDATA%\Arduino15\packages\arduino\hardware\avr\1.6.18\variants (Windows 10 – no idea about *nix, but should be easy enough to locate). So I created my own board variant by making a copy of “standard”, named “standardPB67”. In there, I changed pins_arduino.h as follows (modified lines are highlighted):

Modifying boards.txt

Board definitions can be found in %LOCALAPPDATA%\Arduino15\packages\atmega\hardware\avr\1.3.0 (Windows 10 – but should again be easy enough to locate on other OS’s). I simply added a section to it by copying the original ATmega328-section and changing the prefix to atmega328PB67 (again, important lines highlighted):

Line 15 refers to the name of the subfolder I created in the previous step.

Using in Arduino IDE

After the modifications, when you restart the Arduino IDE, the new board variant shows up:

ATmega328 PB6 & PB7 board
The new board is available

Works… kindof.

The following works:

While the following does not work:

When I try to access a pin digitally by its analog reference (A0 in the example above), it does not work. Initially I changed pins_arduino.h with regard to the analog pin references (which originally started at 14):

This seemed to be logical to me, but this did not work either: The analog references were not working for analogRead! I can’t work out what I do wrong – so if anyone has an idea, please leave a comment. For the time being just remeber to use the numeric references when using digital I/O, and the Ax references for analog inputs.

Update Stability

Modifying boards.txt as given above may not be stable with regard to updates – make sure you have a copy somewhere. At some point I’ll need to find out how to implement my additions properly, following the standards.

Final Remarks

I only needed ATmega328, but following the above scheme, ATmega8 and ATmega168 should work the same way.

I’ll reach out to the community – perhaps someone with more insights can clear up my confusion with regard to the analog pin reference. And perhaps carlosefr will add the pins to his board definitions.

5 thoughts on “Using PB6/7 of ATmega328P with Arduino IDE

  1. change for use I2C
    #define PIN_WIRE_SDA        (18)
    #define PIN_WIRE_SCL        (19) 

    to
    #define PIN_WIRE_SDA        (20)
    #define PIN_WIRE_SCL        (21)

  2. Wow!…

    Minicore does nothing for me. Just ditch the arduino bloatware:

     

    DDRB |= (1 << PB6); 
    // pinMode(PB6, OUTPUT);
    PORTB &= ~(1 << PB6); 
    // digitalWrite(PB6, LOW);

     

  3. An error occurred while uploading the sketch
    avrdude: can’t open device “giveio”

    avrdude: failed to open parallel port “lpt1”

    1. Hi Chaservice,
      I suppose you’re using Windows 10. Under Windows 10 the driver model has significantly changed, and I never got my parallel programmer working again. There are pages on the net that claim that it should work, like e.g. this one, but I never got it working – but I only tried with Bascom, which is a problem in itself. A while ago I bought an USB programmer, and since then my urge to fix this has died away. Another pitfall: You’ll need 64 bit giveio for 64 bit Windows.
      Would be glad to here if and how you fixed it in the end.
      – Hauke

Leave a Reply to chaservice Cancel reply

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

Scroll to top