AZ-Delivery
Published © GPL3+

MicroPython Scale with HX711 - Part 1

This project will show you how to build a highly precise digital scale using a load cell.

IntermediateProtip50
MicroPython Scale with HX711 - Part 1

Things used in this project

Hardware components

D1 Mini NodeMcu with ESP8266-12F WLAN Modul
×1
0,91 Inch OLED I2C Display 128 x 32 Pixel
×1
MB-102 Breadboard
×1
Jumper Wire Cable 3 x 40 STK. je 20 cm M2M/ F2M / F2F
×1
65pcs Jumper Wire Cable Jumpers for Breadboard
×1
Logic Analyzer 8 CH, 24MHz with USB cable
Optional
×1
load cell
×1
HX711
×1

Software apps and online services

Thonny - Python IDE for beginners
Getting started with Thonny: https://www.grzesina.de/az/Die_Entwicklungsumgebung_Thonny_eng.html
DFRobot uPyCraft
Alternative to Thonny

Story

Read more

Schematics

HX711-scale with ESP8266

HX711-scale with ESP32

Code

ssd1306.py

Python
MicroPython SSD1306 OLED driver
# MicroPython SSD1306 OLED driver, I2C and SPI interfaces
# https://www.grzesina.de/az/email/ssd1306.py

from micropython import const
import framebuf


# register definitions
SET_CONTRAST        = const(0x81)
SET_ENTIRE_ON       = const(0xa4)
SET_NORM_INV        = const(0xa6)
SET_DISP            = const(0xae)
SET_MEM_ADDR        = const(0x20)
SET_COL_ADDR        = const(0x21)
SET_PAGE_ADDR       = const(0x22)
SET_DISP_START_LINE = const(0x40)
SET_SEG_REMAP       = const(0xa0)
SET_MUX_RATIO       = const(0xa8)
SET_COM_OUT_DIR     = const(0xc0)
SET_DISP_OFFSET     = const(0xd3)
SET_COM_PIN_CFG     = const(0xda)
SET_DISP_CLK_DIV    = const(0xd5)
SET_PRECHARGE       = const(0xd9)
SET_VCOM_DESEL      = const(0xdb)
SET_CHARGE_PUMP     = const(0x8d)

# Subclassing FrameBuffer provides support for graphics primitives
# http://docs.micropython.org/en/latest/pyboard/library/framebuf.html
class SSD1306(framebuf.FrameBuffer):
    def __init__(self, width, height, external_vcc):
        self.width = width
        self.height = height
        self.external_vcc = external_vcc
        self.pages = self.height // 8
        self.buffer = bytearray(self.pages * self.width)
        super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB)
        self.init_display()

    def init_display(self):
        for cmd in (
            SET_DISP | 0x00, # off
            # address setting
            SET_MEM_ADDR, 0x00, # horizontal
            # resolution and layout
            SET_DISP_START_LINE | 0x00,
            SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0
            SET_MUX_RATIO, self.height - 1,
            SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0
            SET_DISP_OFFSET, 0x00,
            SET_COM_PIN_CFG, 0x02 if self.height == 32 else 0x12,
            # timing and driving scheme
            SET_DISP_CLK_DIV, 0x80,
            SET_PRECHARGE, 0x22 if self.external_vcc else 0xf1,
            SET_VCOM_DESEL, 0x30, # 0.83*Vcc
            # display
            SET_CONTRAST, 0xff, # maximum
            SET_ENTIRE_ON, # output follows RAM contents
            SET_NORM_INV, # not inverted
            # charge pump
            SET_CHARGE_PUMP, 0x10 if self.external_vcc else 0x14,
            SET_DISP | 0x01): # on
            self.write_cmd(cmd)
        self.fill(0)
        self.show()

    def poweroff(self):
        self.write_cmd(SET_DISP | 0x00)

    def poweron(self):
        self.write_cmd(SET_DISP | 0x01)

    def contrast(self, contrast):
        self.write_cmd(SET_CONTRAST)
        self.write_cmd(contrast)

    def invert(self, invert):
        self.write_cmd(SET_NORM_INV | (invert & 1))

    def show(self):
        x0 = 0
        x1 = self.width - 1
        if self.width == 64:
            # displays with width of 64 pixels are shifted by 32
            x0 += 32
            x1 += 32
        self.write_cmd(SET_COL_ADDR)
        self.write_cmd(x0)
        self.write_cmd(x1)
        self.write_cmd(SET_PAGE_ADDR)
        self.write_cmd(0)
        self.write_cmd(self.pages - 1)
        self.write_data(self.buffer)


class SSD1306_I2C(SSD1306):
    def __init__(self, width, height, i2c, addr=0x3c, external_vcc=False):
        self.i2c = i2c
        self.addr = addr
        self.temp = bytearray(2)
        super().__init__(width, height, external_vcc)

    def write_cmd(self, cmd):
        self.temp[0] = 0x80 # Co=1, D/C#=0
        self.temp[1] = cmd
        #print(self.temp,"\n",self.addr)
        self.i2c.writeto(self.addr, self.temp)

    def write_data(self, buf):
        self.temp[0] = self.addr << 1
        self.temp[1] = 0x40 # Co=0, D/C#=1
        self.i2c.start()
        self.i2c.write(self.temp)
        self.i2c.write(buf)
        self.i2c.stop()

oled.py

Python
API for the OLED Display
# https://grzesina.de/az/email/oled.py
"""
#### Datei oled.py
Author: Jürgen Grzesina
Rev.: 1.3 vom 10.07.2021
   added setPixel(), blinkDisplay()

#### Verwendung: from oled import OLED
Die dem Modul ssd1306 zugrunde liegenden grafischen Methoden hat das 
Modul vom Modul framebuf geerbt. Sie sind daher auch nicht, wie erwartet
werden koennte, in ssd1306.py definiert. Eine Dokumentation findet man in 
https://docs.micropython.org/en/latest/library/framebuf.html
Folgende Methoden stehen in der Klasse OLED bereit:
# OLED([sclw=SCLPin],[sdaw=SDAPin],[widthw=[64|128]],[heightw=[32|64]])
# writeAt(string,xpos,ypos, show=True)
# clearFT(xv,yv[,xb=spalte][,yb=zeile], show=True)
# clearAll()
# cursor(x,h,y=0,cset=True,show=True)
# pillar(xpos,breite,hoehe, show=True)
# setKontrast(wert)
# xAxis(show=True), yAxis(show=True)
# switchOn(), switchOff()
# blinkDisplay(cnt,off=0.2,on=0.8)
# position(xp,yp) # dummy Gegenstück zu LCD-Klasse
"""
from machine import Pin
# ssd1306.py muss sich im device directory befinden
from ssd1306 import SSD1306_I2C
import sys
from time import sleep
#
#
class OLED(SSD1306_I2C): 
  device = sys.platform
  if device == 'esp32':
    SD =    21
    SC =    22
  elif device == 'esp8266':
    SD =    4
    SC =    5
  else:
    print("Unbekannter Controller!")
    sys.exit()

  WIDTH = const(128)
  HEIGHT = const(64)
  MaxCol = 16
  MaxRow = 6
  
  def __init__(self, i2c, widthw=WIDTH, heightw=HEIGHT):
    self.columns = widthw // 8 # CPL
    self.rows = heightw // 10 # Lines
    self.i2c = i2c
    self.width = widthw  # Pixel width
    self.height = heightw # Pixel height
    super().__init__(widthw, heightw, self.i2c)
    self.contrast(0x3f) # Maximum ist 0xff
    self.fill(0)
    self.yOffset=0
    self.name="OLED"
    print("this is the constructor of OLED class")
    print("Size:{}x{}".format( self.width, self.height))

  def setYoffset(self,wert):
    if 0<=wert<=4:
        self.yOffset=wert

  def getYoffset(self,wert):
    return self.yOffset

  # Put string s at position col x row y from left upper corner 0; 0
  # for 
  # x = column 0..15
  # y = row 0..[2 | 5]
  def writeAt(self,s,x,y,show=True):
    if x >= self.columns or y >= self.rows: return None
    text = s
    length = len(s)
    xp = x * 8
    yp = y * 10+self.yOffset
    if x+length < self.columns:
      b = length * 8
    else:
      b = (self.columns - x) * 8
      text = text[0:self.columns-x]
    self.fill_rect(xp,yp,b,9,0)
    self.text(text,xp,yp)
    if show:
      self.show()
    xp=x+len(text)
    return (xp if xp <= self.width else None)

  def position(self,xp,yp):
    pass

  def setPixel(self,x,y,c,show=True):
      self.pixel(x,y,c)
      if show:
          self.show()

  def clearAll(self, show=True):
    self.fill(0)
    if show:
      self.show()

  def setContrast(self,k):
    self.contrast(k)
  
  def pillar(self,x,b,h, show=True):
    if x+b > self.width or b<=2 or x < 0: return None
    if h > self.height: h=self.height
    y=self.height-h
    self.fill_rect(x,y,b,h,1)
    if show:
      self.show()
    return h
  
  def cursor(self,x,h,y=0,cset=True,show=True):
    if x >= self.columns or x < 0: return None
    xp = x * 8
    if h > 4: h=4
    if cset:
        self.fill_rect(xp,y,8,h,1)
    else:
        self.fill_rect(xp,y,8,h,0)
    if show:
      self.show()
    return h
  
  def xAxis(self, show=True):
    self.hline(0,self.height-1,self.width,1)
    if show:
      self.show()
  
  def yAxis(self, show=True):
    self.vline(0,0,self.height,1)
    if show:
      self.show()

  def switchOff(self):
    self.poweroff()
  
  def switchOn(self):
    self.poweron()
    
  def blinkDisplay(self,cnt,off=0.2,on=0.8):
      for i in range(cnt):
          self.switchOff()
          sleep(off)
          self.switchOn()
          sleep(on)

  def clearFT(self,x,y,xb=None,yb=None, show=True):
    xv = x * 8
    yv = y * 10+self.yOffset
    if xb==None or xb >= self.columns:
      xb = self.columns*8
    else: 
      xb = (xb+1) *8
    if yb==None or yb >= self.rows:
      yb = self.rows*10+self.yOffset
    else:
      yb = (yb + 1)*10+self.yOffset
    self.fill_rect(xv,yv,xb-xv,yb-yv,0)
    if show:
      self.show()

geometer_30.py

Python
Large font to display numbers
# https://grzesina.de/az/waage/geometer_30.py
chars="0123456789,-"
height=31
width=29
number=[
    (17,  #  Zeichen: 0
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000011111000000,
     0b00001111111110000,
     0b00011111111111000,
     0b00011111111111000,
     0b00111110001111100,
     0b00111110001111100,
     0b01111100000111110,
     0b01111100000111110,
     0b01111100000111110,
     0b01111100000111110,
     0b01111100000111110,
     0b01111100000111110,
     0b01111100000111110,
     0b01111100000111110,
     0b01111100000111110,
     0b00111110001111100,
     0b00111110001111100,
     0b00011111111111000,
     0b00011111111111000,
     0b00001111111110000,
     0b00000011111000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
    ),
    (13,  #  Zeichen: 1
     0b0000000000000,
     0b0000000000000,
     0b0000000000000,
     0b0000111111110,
     0b0001111111110,
     0b0001111111110,
     0b0001111111110,
     0b0000000111110,
     0b0000000111110,
     0b0000000111110,
     0b0000000111110,
     0b0000000111110,
     0b0000000111110,
     0b0000000111110,
     0b0000000111110,
     0b0000000111110,
     0b0000000111110,
     0b0000000111110,
     0b0000000111110,
     0b0000000111110,
     0b0000000111110,
     0b0000000111110,
     0b0000000111110,
     0b0000000111110,
     0b0000000000000,
     0b0000000000000,
     0b0000000000000,
     0b0000000000000,
     0b0000000000000,
     0b0000000000000,
     0b0000000000000,
    ),
    (17,  #  Zeichen: 2
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000111111000000,
     0b00011111111110000,
     0b00111111111111000,
     0b00111111111111000,
     0b01111100011111100,
     0b00111000001111100,
     0b00001000001111100,
     0b00000000001111100,
     0b00000000011111100,
     0b00000000011111000,
     0b00000000111111000,
     0b00000001111110000,
     0b00000001111100000,
     0b00000011111100000,
     0b00000111111000000,
     0b00001111110000000,
     0b00011111100000000,
     0b00011111111111110,
     0b00111111111111110,
     0b01111111111111110,
     0b01111111111111100,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
    ),
    (16,  #  Zeichen: 3
     0b0000000000000000,
     0b0000000000000000,
     0b0000000000000000,
     0b0000011111100000,
     0b0001111111111000,
     0b0011111111111100,
     0b0111111111111110,
     0b0011110001111110,
     0b0001000000111110,
     0b0000000000111110,
     0b0000000011111110,
     0b0000011111111100,
     0b0000011111110000,
     0b0000011111111000,
     0b0000011111111100,
     0b0000000011111110,
     0b0000000000111110,
     0b0010000000111110,
     0b0011000000111110,
     0b0111100001111110,
     0b0111111111111100,
     0b1111111111111100,
     0b0111111111111000,
     0b0000111111100000,
     0b0000000000000000,
     0b0000000000000000,
     0b0000000000000000,
     0b0000000000000000,
     0b0000000000000000,
     0b0000000000000000,
     0b0000000000000000,
    ),
    (17,  #  Zeichen: 4
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000011111000,
     0b00000000111111000,
     0b00000000111111000,
     0b00000001111111000,
     0b00000011111111000,
     0b00000011111111000,
     0b00000111011111000,
     0b00001111011111000,
     0b00001110011111000,
     0b00011100011111000,
     0b00011100011111000,
     0b00111000011111000,
     0b01111000011111000,
     0b01111111111111110,
     0b01111111111111110,
     0b01111111111111110,
     0b01111111111111110,
     0b00000000011111000,
     0b00000000011111000,
     0b00000000011111000,
     0b00000000011111000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
    ),
    (16,  #  Zeichen: 5
     0b0000000000000000,
     0b0000000000000000,
     0b0000000000000000,
     0b0011111111111100,
     0b0011111111111100,
     0b0011111111111000,
     0b0011111111111000,
     0b0011110000000000,
     0b0011110000000000,
     0b0011110000000000,
     0b0011111111100000,
     0b0011111111110000,
     0b0011111111111000,
     0b0011111111111100,
     0b0000000011111110,
     0b0000000001111110,
     0b0000000000111110,
     0b0000000000111110,
     0b0010000000111110,
     0b0111000001111110,
     0b0111111111111100,
     0b1111111111111000,
     0b0111111111110000,
     0b0000111111000000,
     0b0000000000000000,
     0b0000000000000000,
     0b0000000000000000,
     0b0000000000000000,
     0b0000000000000000,
     0b0000000000000000,
     0b0000000000000000,
    ),
    (17,  #  Zeichen: 6
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000111111100,
     0b00000001111111000,
     0b00000001111110000,
     0b00000011111100000,
     0b00000111111000000,
     0b00001111110000000,
     0b00011111100000000,
     0b00011111111100000,
     0b00111111111111000,
     0b00111111111111100,
     0b01111111111111100,
     0b01111110001111110,
     0b01111100000111110,
     0b01111100000111110,
     0b01111100000111110,
     0b01111100000111110,
     0b00111110001111110,
     0b00111111111111100,
     0b00011111111111000,
     0b00001111111110000,
     0b00000011111000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
    ),
    (17,  #  Zeichen: 7
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b01111111111111110,
     0b01111111111111100,
     0b01111111111111100,
     0b01111111111111100,
     0b00000000011111000,
     0b00000000011111000,
     0b00000000011110000,
     0b00000000111110000,
     0b00000000111100000,
     0b00000001111100000,
     0b00000001111100000,
     0b00000011111000000,
     0b00000011111000000,
     0b00000011110000000,
     0b00000111110000000,
     0b00000111110000000,
     0b00001111100000000,
     0b00001111100000000,
     0b00011111000000000,
     0b00011111000000000,
     0b00111110000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
    ),
    (17,  #  Zeichen: 8
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000011111000000,
     0b00001111111110000,
     0b00011111111111000,
     0b00111111111111100,
     0b00111110001111100,
     0b00111110001111100,
     0b00111110001111100,
     0b00111111011111100,
     0b00011111111111000,
     0b00001111111110000,
     0b00011111111111000,
     0b00111111111111100,
     0b01111110001111100,
     0b01111100000111110,
     0b01111100000111110,
     0b01111100000111110,
     0b01111110001111110,
     0b00111111111111100,
     0b00111111111111100,
     0b00011111111111000,
     0b00000111111100000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
    ),
    (17,  #  Zeichen: 9
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000011111000000,
     0b00001111111110000,
     0b00011111111111000,
     0b00111111111111100,
     0b01111110001111100,
     0b01111100000111110,
     0b01111100000111110,
     0b01111100000111110,
     0b01111100000111110,
     0b01111110001111110,
     0b00111111111111110,
     0b00111111111111100,
     0b00011111111111100,
     0b00000111111111000,
     0b00000000111111000,
     0b00000001111110000,
     0b00000011111100000,
     0b00000111111000000,
     0b00001111110000000,
     0b00011111110000000,
     0b00111111100000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
     0b00000000000000000,
    ),
    (8,  #  Zeichen: ,
     0b00000000,
     0b00000000,
     0b00000000,
     0b00000000,
     0b00000000,
     0b00000000,
     0b00000000,
     0b00000000,
     0b00000000,
     0b00000000,
     0b00000000,
     0b00000000,
     0b00000000,
     0b00000000,
     0b00000000,
     0b00000000,
     0b00000000,
     0b00000000,
     0b00000000,
     0b00000000,
     0b00011110,
     0b00111100,
     0b00111100,
     0b00111100,
     0b01111000,
     0b01111000,
     0b01110000,
     0b11110000,
     0b00000000,
     0b00000000,
     0b00000000,
    ),
    (10,  #  Zeichen: -
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0111111110,
     0b0111111110,
     0b0111111110,
     0b0111111110,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
     0b0000000000,
    ),
]

hx711.py

Python
API for the HX711
# https://grzesina.de/az/waage/hx711.py
# hx711.py
# Modul und Klasse zur bedienung des 24-Bit ADC HX711
# Datenblatt: https://datasheetspdf.com/pdf-file/842201/Aviasemiconductor/HX711/1
# 
from time import sleep_us, ticks_ms

class DeviceNotReady(Exception):
    def __init__(self):
        print("Fehler\nHX711 antwortet nicht")


class HX711(DeviceNotReady):
    KselA128 = const(1)
    KselB32 = const(2)
    KselA64 = const(3)
    Dbits =const(24)
    MaxVal = const(0x7FFFFF)
    MinVal = const(0x800000)
    Frame = const(1<<Dbits)
    ReadyDelay = const(3000) # ms
    WaitSleep =const(60) # us
    ChannelAndGain={
        1:("A",128),
        2:("B",32),
        3:("A",64),
        }
    KalibrierFaktor=1104
    
    def __init__(self, dOut, pdSck, ch=KselA128):
        self.data=dOut
        self.data.init(mode=self.data.IN)
        self.clk=pdSck
        self.clk.init(mode=self.clk.OUT, value=0)
        self.channel=ch
        self.tare=0
        self.cal=HX711.KalibrierFaktor
        self.waitReady()
        k,g=HX711.ChannelAndGain[ch]
        print("HX711 bereit auf Kanal {} mit Gain {}".\
              format(k,g))
        
    def TimeOut(self,t):
        start=ticks_ms()
        def compare():
            return int(ticks_ms()-start) >= t
        return compare
    
    def isDeviceReady(self):
        return self.data.value() == 0
    
    def waitReady(self):
        delayOver = self.TimeOut(ReadyDelay)
        while not self.isDeviceReady():
            if delayOver():
                raise DeviceNotReady()
    
    def convertResult(self,val):
        if val & MinVal:
            val -= Frame
        return val
    
    def clock(self):
        self.clk.value(1)
        self.clk.value(0)
    
    def kanal(self, ch=None):
        if ch is None:
            ch,gain=HX711.ChannelAndGain[self.channel]
            return ch,gain
        else:
            assert ch in [1,2,3], "Falsche Kanalnummer: {}\nKorrekt ist 1,2 3".format(ch)
            self.channel=ch
            if not self.isDeviceReady():
                self.waitReady()
            for n in range(Dbits + ch):
                self.clock()
                
    def getRaw(self, conv=True):
        if not self.isDeviceReady():
            self.waitReady()
        raw = 0
        for b in range(Dbits-1):
            self.clock()
            raw=(raw | self.data.value())<< 1 
        self.clock()
        raw=raw | self.data.value()
        for b in range(self.channel):
            self.clock()
        if conv:
            return self.convertResult(raw)

        else:
            return raw
    
    def mean(self, n):
        s=0
        for i in range(n):
            s += self.getRaw()
        return int(s/n) 
    
    def tara(self, n):
        self.tare = self.mean(n)
        return self.tare
        
    def masse(self,n):
        g=(self.mean(n)-self.tare) / self.cal
        return g
        
    def calFaktor(self, f=None):
        if f is not None:
            self.cal = f
        else:
            return self.cal
        
    def wakeUp(self):
        self.clk.value(0)
        self.kanal(self.channel)

    def toSleep(self):
        self.clk.value(0)
        self.clk.value(1)
        sleep_us(WaitSleep)
            
    
    

scale.py

Python
The main code for the scale
# https://grzesina.de/az/waage/scale.py
# scale.py
# After flashing micropython Firmware on the ESP8266:
# import webrepl_setup
# > d for disable
# Then RST; Restart!

# Pintranslator for 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
16 projects • 3 followers
We are "AZ-Delivery - Your Expert for Microelectronics". Visit our shop with regularly new Blogs and free E-Books.
Contact

Comments

Please log in or sign up to comment.