from machine import I2C, Pin, ADC
from time import sleep
import math
import random
# very important
# this modules needs to be saved in the Raspberry Pi Pico in order for the LCD and RTC to be used
from pico_i2c_lcd import I2cLcd
import ds1307
# I2C Initialisation:
# creating I2C objects, specifying the data (SDA) and clock (SCL) pins used in the Raspberry Pi Pico
# any SDA and SCL pins in the Raspberry Pi Pico can be used (check documentation for SDA and SCL pins)
i2c_lcd = I2C(0, sda=Pin(), scl=Pin(), freq=400000)
i2c_rtc = I2C(0, scl=Pin(), sda=Pin(), freq=100000)
# getting I2C addresses
I2C_ADDR_LCD = i2c_lcd.scan()[0]
# I2C objects
# creating an LCD object using the I2C address and specifying number of rows and columns in the LCD
# LCD number of rows = 2, number of columns = 16
lcd = I2cLcd(i2c_lcd, I2C_ADDR_LCD, 2, 16)
# creating an DS1307 RTC object using I2C
ds1307rtc = ds1307.DS1307(i2c_rtc, 0x68)
# Values for Temperature Sensor
# Set Beta ´value for temperature sensor
BETA = 3950
# Set Resistor value for temperature sensor
R1 = 10000
# Set reference voltage for temperature sensor
V_REF = 3.3
# Initialise ADC with temperature sensor connected
adc = ADC(Pin(28))
# Function definitions
# Get temperature function which returns temperature from sensor in celsius as float
# Function uses random number in order to have different values
def get_temperature():
analog_value = adc.read_u16()
voltage = (analog_value / 65535) * V_REF
resistance = R1 / ((V_REF / voltage) - 1)
kelvin = 1 / (1 / 298.15 + 1 / BETA * math.log(resistance / 10000))
celsius = kelvin - 273.15
return round(celsius * get_random_number(), 1)
# Get random number between 0.9 to 1.1 with a chance of 5% that the number is between 0.1 and 1.6
def get_random_number():
if(random.randint(0,100)< 5):
return random.uniform(0.1, 1.6)
else:
return random.uniform(0.9, 1.1)
def main():
while True:
# Implement Assignment
if __name__ == "__main__":
main()