import time
from machine import ADC # Import hardware-related classes: ADC for analog input
from machine import ADC, Pin, I2C # Import hardware-related classes: ADC for analog input, Pin for GPIO, I2C for I2C communication
from i2c_lcd import I2cLcd # Import the custom I2C LCD class from your i2c_lcd.py file
import time # Import time module for using delays
# --- LCD Configuration ---
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000) # Initialize I2C interface (I2C0) on GP0 (SDA) and GP1 (SCL) with 400kHz frequency
lcd = I2cLcd(i2c, 0x27, 2, 16) # Initialize the 16x2 I2C LCD display at address 0x27 (2 rows, 16 columns)
# --- Display Startup Message ---
lcd.clear()
lcd.putstr("ENCM 515 Lab 5")
lcd.move_to(0, 1)
lcd.putstr("Exercise 2")
time.sleep(2)
# --- Gas Sensor Configuration ---
gas_sensor = ADC(26) # Set up ADC on GP26 (ADC0) to read analog voltage from gas sensor
VREF = 3.3 # Reference voltage (3.3V on Pi Pico)
ADC_RES = 65535 # Maximum ADC reading for 16-bit resolution (0 to 65535)
DOLLAR_RATE = 2.50 # Cost rate per volt: $2.50 per volt of gas reading
while True:
# Read analog value and convert to voltage
raw_value = gas_sensor.read_u16()
print('Raw value = ',raw_value)
voltage = (raw_value / ADC_RES) * VREF
print('Voltage = ',voltage)
bill = voltage * DOLLAR_RATE
print('Bill = ',bill)
lcd.move_to(0, 0)
lcd.putstr("Raw Value: {}".format(raw_value))
lcd.move_to(0, 1)
lcd.putstr("Voltage: {:.2f}V".format(voltage))
time.sleep(2)
lcd.clear()
lcd.putstr("Bill: ${:.2f}".format(bill))
time.sleep(2)