Recull

Web de curiosidades y tecnología

Página de Inicio Contacta Las recetas Curiosidades Electrónica Informática Quienes somos.
FRIKI RECULL - Curiosidades y hobbies

BBC micro:bit MakeCode editor extension for HX711 connected to weight sensors


https://github.com/daferdur/pxt-myHX711



HX711 Based on Avia Semiconductor’s patented technology, HX711 is a precision 24-bit analog-to-digital converter (ADC) designed for weigh scales and industrial control applications to interface directly with a bridge sensor.

My codes are based on Arduino sources - 2018 Bogdan Necula Which I've translated from Arduino C to Microbit Makecode.

To import this extension, go to Advanced -> +Extension and enter "myHX711" in the search box, or copy/paste https://github.com/daferdur/pxt-myHX711 into the search box. Press enter and click the extension.

Synopsis

Blocking mode

The library is usually used in blocking mode, i.e. it will wait for the hardware becoming available before returning a reading.

// 1. HX711 circuit wiring Select DOUT_PIN; Select SCK_PIN;

// 2. Adjustment settings and Initialization of the library begin; setscale = 5895655; set_offset = 50682624;

// 3. Acquire reading Serial print("Weight: "); Serial print line(get_units(10));

Non-blocking mode

It is also possible to define a maximum timeout to wait for the hardware to be initialized. This won't send the program into a spinlock when the scale is disconnected and will probably also account for hardware failures.

// 3. Acquire reading without blocking
if (wait_ready_timeout(1000)) {
    Serial print("Weight: ");
    Serial print line(get_units(10));
} else {
    Serial printline("HX711 not found.");
}
}

Features

  1. It provides a tare(N) function, which "resets" the scale to 0. Many other implementations calculate the tare weight when the ADC is initialized only. I needed a way to be able to set the tare weight at any time. Use case: Place an empty container on the scale, call tare(N) to reset the readings to 0, fill the container and get the weight of the content. N could be 10 for example, which will average the tare set.

  2. It provides a power_down() function, to put the ADC into a low power mode. According to the datasheet,

    When PD_SCK pin changes from low to high and stays at high for longer than 60μs, HX711 enters power down mode.

    Use case: Battery-powered scales. Accordingly, there is a power_up() function to get the chip out of the low power mode.

  3. It has a set_gain(byte gain) function that allows you to set the gain factor and select the channel. According to the datasheet,

    Channel A can be programmed with a gain of 128 or 64, corresponding to a full-scale differential input voltage of ±20mV or ±40mV respectively, when a 5V supply is connected to AVDD analog power supply pin. Channel B has a fixed gain of 32.

    The same function is used to select the channel A or channel B, by passing 128 or 64 for channel A, or 32 for channel B as the parameter. The default value is 128, which means "channel A with a gain factor of 128", so one can simply call set_gain().

    This function is also called from the initializer method begin().

  4. The get_value() and get_units() functions can receive an extra parameter "times", and they will return the average of multiple readings instead of a single reading.

How to calibrate your load cell

  1. Call set_scale(Cell Full Scale), for example, for a 2kg cell, start with 2000.

  2. Call tare(10) to tare it with 10 readings.

  3. Place a known weight on the scale and call get_units(10).

  4. Divide the result in step 3 to your known weight. You should get about the parameter you need to pass to set_scale(XX).

  5. Adjust the parameter in step 4 until you get an accurate reading.

Credits

Thanks to Weihong Guan who started the first version of this library in 2012 already (see [arduino|module]Hx711 electronic scale kit, sources), Bogdan Necula who took over in 2014 and last but not least all others who contributed to this library over the course of the last years.

See also

Blocks

The extension can be used for HX711 modules.

BASIC EXAMPLE (get reading): 1

HX711.SetPIN_DOUT(PIN_DOUT.P0)
HX711.SetPIN_SCK(PIN_PD_SCK.P8)
HX711.begin()
serial.redirect(SerialPin.USB_TX,SerialPin.USB_RX,BaudRate.BaudRate9600)
basic.forever(function()
serial.writeString("HX711 reading: ")
serial.writeNumber(HX711.get_scale())
serial.writeLine("")
basic.pause(1000)
})



FULL EXAMPLE: (almost all commands) 2

let valor_string = ""
let ceros = ""
let valor: number = 0
HX711.SetPIN_DOUT(DigitalPin.P0)
HX711.SetPIN_SCK(DigitalPin.P8)
HX711.begin()
serial.redirect(
SerialPin.USB_TX,
SerialPin.USB_RX,
BaudRate.BaudRate9600
)
serial.writeLine("HX711 Initializing Scale: ")
serial.writeLine("Before Setting Up the Scale: ")
serial.writeString("Read: ")
serial.writeLine("" + HX711.read())
serial.writeString("Read Average: ")
serial.writeLine("" + HX711.read_average(20))
serial.writeString("Get Value: ")
serial.writeLine("" + HX711.get_value(5))
serial.writeString("Get Units: ")
serial.writeLine("" + HX711.get_units(5))
HX711.set_scale(2280)
HX711.tare(1)
basic.pause(1000)
serial.writeString("Read:")
serial.writeLine("" + HX711.read())
serial.writeString("Read Average:")
serial.writeLine("" + HX711.read_average(20))
serial.writeString("Get Value:")
serial.writeLine("" + HX711.get_value(5))
serial.writeString("Get Units")
serial.writeLine("" + HX711.get_units(5))
serial.writeLine("")
serial.writeLine("")
serial.writeLine("")
serial.writeLine("")
serial.writeLine("Readings: ")
basic.pause(1000)
basic.forever(function () {
    serial.writeString("One Reading: ")
    valor = HX711.get_units(1)
    ceros = ""
    if (Math.abs(Math.round((valor - Math.trunc(valor)) * 100)).toString().length == 0) {
        ceros = "00"
    } else if (Math.abs(Math.round((valor - Math.trunc(valor)) * 100)).toString().length == 1) {
        ceros = "0"
    }
    valor_string = "" + Math.trunc(valor) + "." + ceros + Math.abs(Math.round((valor - Math.trunc(valor)) * 100))
    serial.writeLine(valor_string)
    serial.writeString("Average: ")
    valor = HX711.get_units(20)
    ceros = ""
    if (Math.abs(Math.round((valor - Math.trunc(valor)) * 100)).toString().length == 0) {
        ceros = "00"
    } else if (Math.abs(Math.round((valor - Math.trunc(valor)) * 100)).toString().length == 1) {
        ceros = "0"
    }
    valor_string = "" + Math.trunc(valor) + "." + ceros + Math.abs(Math.round((valor - Math.trunc(valor)) * 100))
    serial.writeLine(valor_string)
    HX711.power_down()
    basic.pause(4000)
    HX711.power_up()
    basic.pause(100)
})






Sensor wiring

The Load Cell Amplifier is a small breakout board for the HX711 IC that allows you to easily read load cells to measure weight. By connecting the amplifier to your microcontroller you will be able to read the changes in the resistance of the load cell and with some calibration you’ll be able to get very accurate weight measurements. This can be handy for creating your own industrial scale, process control, or simple presence detection of an object.

The HX711 uses a two wire interface (Clock and Data) for communication. Any microcontroller’s GPIO pins should work and numerous libraries have been written making it easy to read data from the HX711.

Load cells use a four wire wheatstone bridge to connect to the HX711. These are commonly colored RED, BLK, WHT, GRN. Each color corresponds to the conventional color coding of load cells:

If you invert White & Green wires you'll get reading with negative sign.

License

MIT

Supported targets

TO DO