Getting started with the AVR-DB on a breadboard

If you are familiar with prototyping Atmega328 based circuits on a breadboard, using the AVR-DB 28-pin through-hole package is very similar.

This is how I started with the process of familiarising myself with this chip before putting together the full emonTx v4 design. The very first step of course is to get an LED to flash! (The Arduino Blink example)

Through hole AVR-DB microcontrollers are available via standard distributors e.g Farnell: https://uk.farnell.com/microchip/avr128db28-i-sp/mcu-8bit-24mhz-spdip-28/dp/3594413?st=avr-db or from microchip direct (cheaper in bulk): https://www.microchipdirect.com/product/search/all/avr128db28

Blink

1. Breadboard layout

In the following breadboard circuit we are just routing power and ground to the relevant pins, the decoupling capacitors are there as suggested for best practice. The LED is connected to PA7 (pin 1) via a current limiting resistor and the programming of the AVR-DB chip is done using the UPDI programming interface.

Download the AVR-DB Datasheet here: AVR® DB | Microchip Technology

UPDI is a single pin programming interface, Im using a simple resistor and diode to convert the USB to UART programmer into a UPDI programmer. Spence Konde has written a very useful guide on SerialUDPI here: AVR-Guidance/jtag2updi.md at master · SpenceKonde/AVR-Guidance · GitHub.

Spence Konde’s documentation for the AVR-DB chip in general is top notch and he deserves every credit for making this chip accessible to those of us who like using the Arduino IDE.

2. Setting up the Arduino IDE

If you don’t already have the Arduino IDE it can be downloaded from https://www.arduino.cc/en/software

Once you have the IDE installed, you then need to install Spence Konde’s DxCore. This can be done by first pasting the following board manager URL in Arduino IDE > File > Preferences:

http://drazzy.com/package_drazzy.com_index.json

Then navigating to: Tools > Boards > Boards Manager, Select “DxCore by Spence Konde” and click Install.

3. The blink program:

The following basic blink program is all that you need to get blink working:

void setup() {
  pinMode(PIN_PA7, OUTPUT);
}

void loop() {
  digitalWrite(PIN_PA7, HIGH);
  delay(100);
  digitalWrite(PIN_PA7, LOW);
  delay(100);
}

4. Upload settings:

There are quite a few compilation and upload settings that can be set with this chip.

  • If you are uploading using SerialUDPI you need to select a (no bootloader) option.
  • Make sure to select the right chip variant that you are using e.g AVR128DB28 in this instance.
  • Im using the internal clock for this example so have the “24 Mhz Internal” clock option selected.
  • The rest are all default settings but are detailed in a lot of depth in Spence Konde’s documentation for those interested in delving into these further.

5. Upload result:

image

That’s it! Your LED should be flashing away! Hopefully that gives a quick overview of what it’s like using these chips, most of what you are familiar with from Arduino (ATmega’s) is transferable.

Not all libraries are supported yet as there are some low level code differences, the AVR-DB range has many similarities to the low level code used by the ATMega4809, which is used in the official Arduino Nano Every in terms of things like low level ADC code etc. The work required to get things running such as the continuous sampling code used by the emonLibCM library is however relatively straightforward and I will post the details of this for those interested in a follow up post.

1 Like

Basic breadboard electricity monitor using the AVR128DB28

First draft: I will probably improve this guide over time, if you have any questions about aspects that are not particularly clear, please ask!

Building on the basic AVD-DB Blink example above, the following circuit adds a voltage sensing channel and a current sensing CT channel. The circuit is very similar to the one documented in our Learn: How to build an Arduino energy monitor page, with one simple voltage divider and decoupling capacitor bias connected a voltage output 333mV CT and another connected to a ZMPT precision voltage sensor (An ACAC adapter could alternatively be used).

Voltage sensing with the ZMPT101

The ZMPT101 precision voltage sensor provides isolation between the mains voltage and the low voltage side. Resistors R1,R2,R3,R4,R5 & R6 below are all 33k resistors and form both a layer of protection in the event of short circuit to one of the resistors as well as forming a voltage divider that reduces the current through the primary down to the required 1-2mA range.

PLEASE TAKE EXTRA CARE IF YOU BUILD THIS CIRCUIT GIVEN THE POTENTIALLY LETHAL HIGH VOLTAGE INVOLVED!! If you have any doubt, please use the AC-AC Adapter approach to avoid handling of any high voltage.

Across the secondary is a 200 Ohm burden resistor providing an RMS voltage output of about 242mV.

This is biased with the simple voltage divider, the top resistor is 18k and the bottom resistor 3.3k, providing a bias of 0.511V, near enough half 1.024V (I am using the 1.024V internal ADC reference in this example).

Current sensing with a voltage output CT

The circuit for the CT input is very simple and just consists of the voltage divider bias connected to the CT sensor, the top resistor is again 18k and the bottom resistor 3.3k, providing the 0.511V bias. We again have a 10uF decoupling capacitor.

Basic firmware example using emonLibCM (avrdb branch)

The following basic example uses a very slightly modified version of @Robert.Wall’s emonLibCM library.

The changes are in the ‘avrdb’ branch and can be seen here: Comparing master...avrdb · openenergymonitor/EmonLibCM · GitHub. To use this branch, either use git to clone the repository and then ‘git checkout avrdb’ to switch to the branch, or download the branch directly from github itself.

#include <Arduino.h>
#include "emonLibCM.h"

void setup() 
{
  Serial.pins(PIN_PA4, PIN_PA5);
  Serial.begin(115200);
  Serial.println("\nBreadboard AVR-DB electricity monitor demo"); 

  // 12 bit ADC = 4096 divisions
  // Time in microseconds for one ADC conversion: 40 us 
  EmonLibCM_setADC(12,40);

  // Using AVR-DB 1.024V internal voltage reference
  EmonLibCM_ADCCal(1.024);

  // Voltage input calibration
  // (6 x 33000) / 200 = 990.0
  EmonLibCM_SetADC_VChannel(1, 990.0);

  // Current input calibration
  // 50A / 0.333V = 150.15
  EmonLibCM_SetADC_IChannel(2, 150.15, 0);
  
  EmonLibCM_Init();
  EmonLibCM_datalog_period(5);
  
}

void loop()             
{
  if (EmonLibCM_Ready())   
  {
    Serial.println(EmonLibCM_acPresent()?"AC present ":"AC missing ");

    delay(50);
 
    Serial.print("V=");Serial.print(EmonLibCM_getVrms());
    Serial.print(" I=");Serial.print(EmonLibCM_getIrms(0),3);
    Serial.print(" W=");Serial.print(EmonLibCM_getRealPower(0));
    Serial.print(" VA=");Serial.print(EmonLibCM_getApparentPower(0));
    Serial.print(" Wh=");Serial.print(EmonLibCM_getWattHour(0));
    Serial.print(" pf=");Serial.print(EmonLibCM_getPF(0),4);      
    Serial.println();
  }
}

Example output:

I really must emphasise the warning above:

UNLESS YOU ARE TRAINED AND EXPERIENCED IN WORKING ON LIVE HIGH VOLTAGE EQUIPMENT, DO NOT USE TRYSTAN’S CIRCUIT

Your prototype board or stripboard WILL NOT BE RATED FOR MAINS VOLTAGES. You must use a custom p.c.b. with the appropriate clearances around, under and where appropriate between every component on the high voltage side, and underneath the transformer between high and low voltage sides, and you must fully shroud every part of the high voltage side to prevent contact.

I don’t want to stop anyone experimenting, but it would be nice if you’re still alive afterwards to report progress.

2 Likes

I will change the above guide to give an example using an AC AC adapter, I have a small custom PCB below that ZMPT101 to handle the described circuit, keeping all live voltages off and isolated from the breadboard, but that’s not something that’s generally available so it’s probably best to keep the guide to using an ACAC adapter.

Using the ZMPT101 is not really that important for the purposes of the guide above.