A Microcontroller-Based Scale: Connecting the HX711 to an Atmega16.

This scale has been sitting unused for a long time.
A Microcontroller-Based Scale: Connecting the HX711 to an Atmega16.

One day it simply stopped turning on, and my attempt to repair it failed, so I decided to restore it and at the same time figure out how it works. For this I ordered an HX711 board from AliExpress.
A Microcontroller-Based Scale: Connecting the HX711 to an Atmega16.

This chip contains a programmable gain amplifier and a sigma-delta ADC; it was specifically designed for use in scales.

Pin functions:
  • VCC, GND - power
  • DT, SCK — data pins
  • E+, E- — load cell bridge excitation
  • A+, A- — channel A differential input
  • B+, B- — channel B differential input


The block diagram looks like this.
A Microcontroller-Based Scale: Connecting the HX711 to an Atmega16.


Now let's take a closer look at the parameters we're interested in
  • 2-channel, 24-bit ADC with a differential input
  • programmable gain of 32, 64 or 128
  • selectable sample rate of 10 or 80 samples per second
  • supply voltage range from 2.7 to 5.5 volts


2 pins are used to get the data: DT and SCK. During conversion, DT is at logic one, and SCK must be at logic zero.

When the conversion is complete, a logic zero appears on DT, indicating that the data can be read. Then, on each rising edge at the SCK input, the data is shifted out one bit at a time, MSB first. When reading is finished, DT is set to logic one. That's the whole protocol.

As for the gain and channel selection, they depend on the number of pulses on the SCK pin, as shown in the table below.
A Microcontroller-Based Scale: Connecting the HX711 to an Atmega16.

And I should say that the gain is always programmed for the next sample. That is, if, while reading the current value, we apply 26 pulses to the SCK pin, the next value we read will be a sample from channel B with a gain of 32.

The measurable voltage range depends on the gain: with a gain of 128 the measured voltage can vary within ±20mV, with a gain of 64 — ±40mV, and with a gain of 32 — ±80mV, assuming the AVDD pin (analog supply) is at 5 volts. If the input signal goes below the lower limit, the ADC will output 800000h; above the upper limit, 7FFFFFh.

The sample rate depends on the voltage on the RATE pin and on the clock frequency. When the chip is clocked from its internal oscillator, which is the case we're interested in, the sample rate is 10 per second with RATE=0 and 80 per second with RATE = DVDD (digital supply); on the board this pin is tied to ground.

The last pages of the datasheet give an implementation of the protocol in C-51 and assembly; let's adapt it for the Atmega16.

#define F_CPU 8000000UL

#include <avr/io.h>

#define HX_PORT PORTB
#define HX_DDR  DDRB
#define HX_PIN  PINB

#define data 0
#define sck 1
//weighing result
int32_t adc_value = 0;

void HX711_Init(void)
{   
	//sck output, data - input
	HX_DDR |= (1<<sck);
	HX_DDR &= ~(1<<data);
	
	//pull data up to the supply, set sck to logic one
	HX_PORT |= (1<<data);
	HX_PORT &= ~(1<<sck);
}

uint32_t Weighing(void)
{
	uint8_t i = 0;
	adc_value = 0;

	HX_PORT &= ~(1<<sck);
	//wait until the conversion is finished
	while((HX_PIN & (1<<data)));
	
	for(i=0; i<24; i++)
	{
		//raise the strobe
		HX_PORT |= (1<<sck);
		//shift the ADC value left; now the ADC value looks like .......0
		adc_value <<= 1;
		//lower the strobe
		HX_PORT &= ~(1<<sck);
		//check what is on the data pin; if zero, the rightmost bit stays zero .......0
		//if 1, the rightmost bit will be one .......1
		if(HX_PIN & (1<<data))
		{
			adc_value++;
		}	
	}

	//we'll take samples from channel A with a gain of 128
	//so we strobe one more time
	HX_PORT |= (1<<sck);
	HX_PORT &= ~(1<<sck);
	
	return adc_value;
}

int main(void)
{

    HX711_Init();

    while(1)
    {
          Weighing(); 
    }
}

The wiring diagram is shown below.
A Microcontroller-Based Scale: Connecting the HX711 to an Atmega16.


Now let's connect the load cell bridge taken from the scale.
A Microcontroller-Based Scale: Connecting the HX711 to an Atmega16.

A Microcontroller-Based Scale: Connecting the HX711 to an Atmega16.

This value is the weight of the platform; we'll always subtract it from the result.

Let's put a 9V battery on the scale.
A Microcontroller-Based Scale: Connecting the HX711 to an Atmega16.

A Microcontroller-Based Scale: Connecting the HX711 to an Atmega16.


Let's add another one.
A Microcontroller-Based Scale: Connecting the HX711 to an Atmega16.

A Microcontroller-Based Scale: Connecting the HX711 to an Atmega16.


When measuring the same load, the values differed by several hundred counts, but the result is still excellent.
All that's left is to find a couple of weights for calibration and decide where to output the result: to an LCD display or to a tablet via a Bluetooth module; but that's all for now.

HX711 datasheet