from utime import sleep
from hx711 import HX711
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import neopixel
# OLED configuration
WIDTH = 128
HEIGHT = 64
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=200000)
oled = SSD1306_I2C(WIDTH, HEIGHT, i2c)
# Pressure sensor configuration
capteur_hx711 = HX711(4, 5, 1)
# Calibration factor for converting sensor readings to kilograms
CALIBRATION_FACTOR = 420
# NeoPixel configuration
NEOPIXEL_PIN = 14 # D14 pin on ESP32
NUM_PIXELS = 10 # Number of NeoPixels
np = neopixel.NeoPixel(machine.Pin(NEOPIXEL_PIN), NUM_PIXELS)
# Define mass thresholds for lighting up NeoPixels
MASS_THRESHOLDS = [4, 8, 12, 16] # Mass thresholds in kilograms
# Function to convert sensor reading to kilograms
def convert_to_kg(raw_reading):
return raw_reading / CALIBRATION_FACTOR
# Function to light up NeoPixels based on mass
def light_up_neopixels(mass_kg):
for i in range(NUM_PIXELS):
if mass_kg > MASS_THRESHOLDS[i]:
np[i] = (255, 255, 255) # White color
else:
np[i] = (0, 0, 0) # Turn off NeoPixel
np.write()
while True:
capteur_hx711.power_on()
# Wait until the sensor is ready
while not capteur_hx711.is_ready():
pass
# Read the sensor value
raw_reading = capteur_hx711.read()
# Convert the raw sensor reading to kilograms
mass_kg = convert_to_kg(raw_reading)
# Clear the OLED display
oled.fill(0)
# Display the mass in kilograms on the OLED display
oled.text("Mass (kg):", 10, 10)
oled.text("{:.2f}".format(mass_kg), 10, 30)
# Update the OLED display
oled.show()
# Light up NeoPixels based on mass
light_up_neopixels(mass_kg)
# Wait for a short period before taking the next reading
sleep(1)