It's easy to use the HX711 with Python or CircuitPython, and the Adafruit_CircuitPython_HX711 module. This module allows you to easily write Python code to read data from the ADC.
You'll need a strain gauge to use this example with the breakout:
CircuitPython Microcontroller Wiring
First, wire up a strain gauge to the breakout exactly as follows. Then, wire up the breakout to your board. The following is the breakout wired to a Feather RP2040:
- Strain gauge power to breakout E+ (red wire)
- Strain gauge ground to breakout E- (black wire)
- Strain gauge positive to breakout A+ (green wire)
- Strain gauge negative to breakout A- (white wire)
- Board 3V to breakout VIN (red wire)
- Board GND to breakout GND (black wire)
- Board D5 to breakout DATA (blue wire)
- Board D6 to breakout SCK (yellow wire)
CircuitPython Usage
To use with CircuitPython, you need to first install the Adafruit_CircuitPython_HX711 library into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY/lib folder should contain the following folder:
- /adafruit_hx711
Example Code
Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries # # SPDX-License-Identifier: MIT import time import board import digitalio from adafruit_hx711.hx711 import HX711 from adafruit_hx711.analog_in import AnalogIn data = digitalio.DigitalInOut(board.D5) data.direction = digitalio.Direction.INPUT clock = digitalio.DigitalInOut(board.D6) clock.direction = digitalio.Direction.OUTPUT hx711 = HX711(data, clock) channel_a = AnalogIn(hx711, HX711.CHAN_A_GAIN_128) # channel_b = AnalogIn(hx711, HX711.CHAN_B_GAIN_32) while True: print(f"Reading: {channel_a.value}") time.sleep(1)
First, the HX711 is instantiated with its clock and data pins. In the loop, the A channel on the ADC is read and printed to the serial console. As you bend, twist and put pressure on the strain gauge, you'll see the values change.
Text editor powered by tinymce.