# Project: analog_sensor input wigth output on LCD screen.
# Project Description:
# This sketch writes the readings read from analog_sensor onto a LCD screen.
# This sketch is for a Raspberrypico and LCD 4x20
#
# Author: STEMVentor Educonsulting
#
# This code is copyrighted. Please do not reuse or share for any purpose other than for learning with subscribed STEMVentor programs.
# This code is for educational purposes only and is not for production use.
# LIBRARIES
# The machines library is a basic library which is built in the raspberry pico.
# It lets one use access and control the pins of the pico using basic functions and commands
from machine import I2C, Pin , ADC
from time import sleep #so that sleep funciton can be used , similar to delay in arduino ide.
from pico_i2c_lcd import I2cLcd
# PIN DEFINITIONS
analog_sensor_pin = 28
# INITIALIZE OBJECTS
i2c_connection = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
I2C_ADDR = i2c_connection.scan()[0] #to get the I2C address
lcd = I2cLcd(i2c_connection, I2C_ADDR, 4, 20) #create lcd object
analog_sensor_adc = ADC(Pin(analog_sensor_pin))
# LOCAL FUNCTIONS
def printByRow( string , row_number): #row number is from 0-4
lcd.move_to(0 , row_number)
lcd.putstr(" ") #print 20 blank spaces to clear the row
lcd.move_to(0 , row_number)
lcd.putstr(string)
# MAIN CODE
print(I2C_ADDR)
lcd.backlight_on()
printByRow("analog sensor value: " , 0)
while True:
value = analog_sensor_adc.read_u16()
print(" analog_sensor value : " , value)
printByRow(str(value) , 2)
sleep(1)