AZ-Delivery
Published © GPL3+

Digital scale with HX711 and ESP8266 / ESP32 in Micropython

A scale without moving parts – impossible, you say? Not according to the result of my current project, which boasts incredible resolution.

AdvancedFull instructions provided199
Digital scale with HX711 and ESP8266 / ESP32 in Micropython

Things used in this project

Hardware components

D1 Mini Nodemcu with ESP8266-12F WLAN module
or D1 Mini V3 Nodemcu with ESP8266-12F or Nodemcu Lua Amica Module V2 ESP8266 ESP-12F WiFi or Nodemcu Lua Lolin V3 Module ESP8266 ESP-12F WIFI or ESP32 Dev Kit C unpleasant or ESP32 Dev Kit C V4 unplacerated or ESP32 NODEMCU Module WiFi Development Board with CP2102 or Nodemcu-ESP-32S kit or ESP32 Lolin Lolin32 WiFi Bluetooth Dev Kit
×1
0.91 inch OLED I2C Display 128 x 32 pixels
×1
Weighing cell 1kg
×1
HX711 AD converter for weighing cells
×1
MB-102 Breadboard Pug with 830 contacts
×1
Jumper Wire cable 3 x 40 pcs. 20 cm M2M / F2M / F2F each
×1
65stk. Jumper wire cable plug -in bridges for Breadboard
×1
Logic Analyzer
optional
×1

Software apps and online services

Thonny
or µpycraft
Used firmware for the ESP32: V1.19.1 (2022-06-18).
Used firmware for the ESP8266: V1.19.1 (2022-06-18).
Hardware driver for the OLED display
oled.py API for the OLED display
geometer_30.py Large character set for the digit display
hx711.py API for the AX711
scale.py The operating program
zeichensatz.rar Working environment for creating your own symbols

Story

Read more

Code

Code

Python
# scale.py
# Nach dem Flashen der Firmware auf dem ESP8266:
# import webrepl_setup
# > d fuer disable
# Dann RST; Neustart!

# Pintranslator fuer ESP8266-Boards
# LUA-Pins     D0 D1 D2 D3 D4 D5 D6 D7 D8
# ESP8266 Pins 16  5  4  0  2 14 12 13 15 
#                 SC SD
# used             I2C   

from machine import Pin,freq,SoftI2C
from time import sleep
from oled import OLED
import geometer_30 as cs
import sys
from hx711 import HX711

if sys.platform == "esp8266":
    i2c=SoftI2C(scl=Pin(5),sda=Pin(4),freq=400000)
    freq(160000000)
elif sys.platform == "esp32":
    i2c=SoftI2C(scl=Pin(22),sda=Pin(21),freq=400000)
    freq(240000000)
else:
    raise UnkownPortError()

d=OLED(i2c,128,32)
d.clearAll()

dout=Pin(14) # D5
dpclk=Pin(2) # D4
delta = 0.8
taste=Pin(0,Pin.IN,Pin.PULL_UP) # D3

def putNumber(n,xpos,ypos,show=True):
    breite=cs.number[n][0]
    for row in range(1,cs.height):
        for col in range(breite-1,-1,-1):
            c=cs.number[n][row] & 1<<col
            d.setPixel(xpos+breite+3-col,ypos+row,c,False)
    if show:
        d.show()
    return xpos+breite+2

pos=0
try:
    hx = HX711(dout,dpclk)
    for n in range(8):
        pos=putNumber(11,pos,0)
    hx.wakeUp()
    hx.kanal(1)
    hx.tara(25)
    hx.calFaktor(hx.calFaktor()+delta)
    print("Waage gestartet")
except:
    print("HX711 initialisiert nicht")
    for n in range(8):
        pos=putNumber(0,pos,0)
    sys.exit()
    
while 1:
    if taste.value() == 0:
        d.clearAll(False)
        pos=0
        for n in range(14):
            pos=putNumber(10,pos,0)
        hx.tara(25)
    m="{:0.2f}".format(hx.masse(10))
    d.clearAll(False)
    pos=0
    m=m.replace(".",",")
    for i in range(len(m)-1):
        z=m[i]
        n=cs.chars.index(z)
        pos=putNumber(n,pos,0, False)
    z=m[len(m)-1]
    n=cs.chars.index(z)
    pos=putNumber(n,pos,0)
    state=(taste.value() == 0)
    sleep(0.5)
#     if state and taste.value() == 0:
#         d.clearAll()
#         d.writeAt("Program canceled",0,0)
#         print("Programm beendet")
#         sys.exit()
    
        

Credits

AZ-Delivery
23 projects • 3 followers
We are "AZ-Delivery - Your Expert for Microelectronics". Visit our shop with regularly new Blogs and free E-Books.

Comments