Skip to content

Commit 109eebc

Browse files
authored
Merge pull request #2969 from jedgarpark/espresso-water
updated espresso code to power down sensor
2 parents e5757cc + d64251b commit 109eebc

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

Espresso_Water_Meter/code.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import socketpool
1515
import wifi
1616
import board
17+
import digitalio
1718
import alarm
1819
import neopixel
1920
import adafruit_hcsr04
@@ -22,6 +23,20 @@
2223
import adafruit_requests
2324
import adafruit_max1704x
2425

26+
# Initialize the power pin for the sensor
27+
sensor_power = digitalio.DigitalInOut(board.A2)
28+
sensor_power.direction = digitalio.Direction.OUTPUT
29+
sensor_power.value = False # Start with sensor powered off
30+
31+
def power_sensor_on():
32+
"""Turn on power to the ultrasonic sensor and wait for it to stabilize."""
33+
sensor_power.value = True
34+
time.sleep(0.55) # Give sensor time to power up and stabilize
35+
36+
def power_sensor_off():
37+
"""Turn off power to the ultrasonic sensor."""
38+
sensor_power.value = False
39+
2540
# Initialize the sonar sensor
2641
sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.A0, echo_pin=board.A1)
2742

@@ -46,9 +61,9 @@
4661

4762
# Operating hours (24-hour format with minutes, e.g., "6:35" and "16:00")
4863
OPENING_TIME = "6:00"
49-
CLOSING_TIME = "22:30"
64+
CLOSING_TIME = "15:30"
5065
# Normal operation check interval
51-
NORMAL_CHECK_MINUTES = 5
66+
NORMAL_CHECK_MINUTES = 10
5267
# Sleep duration in seconds during operating hours
5368
SLEEP_DURATION = 60 * NORMAL_CHECK_MINUTES
5469
# Display duration in seconds
@@ -64,6 +79,7 @@ def parse_time(time_str):
6479

6580
def get_average_distance():
6681
"""Take multiple distance readings and return the average."""
82+
power_sensor_on() # Power on the sensor before taking measurements
6783
distances = []
6884
for _ in range(NUM_SAMPLES):
6985
try:
@@ -73,6 +89,7 @@ def get_average_distance():
7389
except RuntimeError:
7490
print("Error reading distance")
7591
continue
92+
power_sensor_off() # Power off the sensor after measurements
7693

7794
# Only average valid readings
7895
if distances:
@@ -248,6 +265,7 @@ def set_pixel_color(distance):
248265
sleep_seconds = SLEEP_DURATION # Use normal interval if we couldn't get readings
249266

250267
# Prepare for deep sleep
268+
power_sensor_off() # Make sure sensor is powered off before sleep
251269
pixel.brightness = 0 # Turn off NeoPixel
252270

253271
# Flush the serial output before sleep

0 commit comments

Comments
 (0)