First working code for HC-SR04 sensor.
This commit is contained in:
68
pico_funcs.py
Normal file
68
pico_funcs.py
Normal file
@@ -0,0 +1,68 @@
|
||||
import machine
|
||||
import network
|
||||
import wifi_config
|
||||
import time
|
||||
|
||||
'''
|
||||
pico_funcs: Helper functions for things the pico can do.
|
||||
'''
|
||||
|
||||
def read_cpu_temp():
|
||||
"""
|
||||
If you print the value of the temperature value you are going to get an integer number between 0 and 65535.
|
||||
So, we have to convert this value either to the Celsius degree scales.
|
||||
|
||||
The temperature sensor works by delivering a voltage to the ADC4 pin that is proportional to the temperature.
|
||||
From the datasheet, a temperature of 27 degrees Celsius delivers a voltage of 0.706 V.
|
||||
With each additional degree the voltage reduces by 1.721 mV or 0.001721 V.
|
||||
The first step in converting the 16-bit temperature is to convert it back to volts, which is done based on the 3.3 V maximum voltage used by the Pico board.
|
||||
ref: https://how2electronics.com/read-temperature-sensor-value-from-raspberry-pi-pico/
|
||||
"""
|
||||
cpu_temp_conversion_factor = 3.3 / 65535
|
||||
cpu_temp_sensor = machine.ADC(4)
|
||||
reading = cpu_temp_sensor.read_u16() * cpu_temp_conversion_factor
|
||||
temperature_c = 27 - (reading - 0.706) / 0.001721
|
||||
temperature_f = temperature_c * 9/5. + 32.0
|
||||
return temperature_f
|
||||
|
||||
def wlan_up(wlan):
|
||||
wlan.active(True)
|
||||
wlan.connect(wifi_config.HOME_WIFI_SSID, wifi_config.HOME_WIFI_PWD)
|
||||
|
||||
# Wait for connect or fail
|
||||
max_wait = 10
|
||||
while max_wait > 0:
|
||||
if wlan.status() < 0 or wlan.status() >= 3:
|
||||
break
|
||||
max_wait -= 1
|
||||
time.sleep(1)
|
||||
|
||||
if max_wait == 0:
|
||||
return None
|
||||
|
||||
ifconfig = wlan.ifconfig()
|
||||
return ifconfig
|
||||
|
||||
def led_error_code(led, error_code: int):
|
||||
"""Blink LED for a given error code (int). error code == number of times to blink"""
|
||||
|
||||
# Run a quick 'start error code sequence'
|
||||
# So we know when LED error sequence starts
|
||||
start_sequence_counter = 0
|
||||
while start_sequence_counter < 3:
|
||||
led.value(True)
|
||||
time.sleep(0.1)
|
||||
led.value(False)
|
||||
time.sleep(0.1)
|
||||
start_sequence_counter += 1
|
||||
|
||||
# Run real error code sequence
|
||||
blink_counter = 0
|
||||
while blink_counter < error_code:
|
||||
time.sleep(1)
|
||||
led.value(True)
|
||||
time.sleep(1)
|
||||
led.value(False)
|
||||
blink_counter += 1
|
||||
# Make sure to turn off LED when this subroutine finished
|
||||
led.value(False)
|
||||
Reference in New Issue
Block a user