import machine
import onewire
import ds18x20
import time
from machine import Pin, ADC
# Setup DS18B20 temperature sensor
dat = machine.Pin(4) # GPIO4 for DS18B20
ds_sensor = ds18x20.DS18X20(onewire.OneWire(dat))
# Setup ADC for Potentiometers (simulating pH and turbidity sensors)
ph_sensor = ADC(Pin(34)) # GPIO34 (Analog input) for pH sensor
ph_sensor.atten(ADC.ATTN_11DB) # Set range 0-3.3V for better precision
turbidity_sensor = ADC(Pin(35)) # GPIO35 (Analog input) for turbidity sensor
turbidity_sensor.atten(ADC.ATTN_11DB) # Set range 0-3.3V for better precision
# Scan for DS18B20 devices
roms = ds_sensor.scan()
print('Found DS devices:', roms)
while True:
# Temperature reading from DS18B20
ds_sensor.convert_temp() # Convert temperature
time.sleep_ms(750) # Wait for conversion to complete
temp = ds_sensor.read_temp(roms[0]) # Read temperature value from the sensor
# pH sensor reading (simulated by potentiometer)
ph_value = ph_sensor.read() # Read analog value from potentiometer
ph = (ph_value / 4095) * 14 # Convert ADC value to pH scale (0-14)
# Turbidity sensor reading (simulated by potentiometer)
turbidity_value = turbidity_sensor.read() # Read analog value from potentiometer
turbidity = (turbidity_value / 4095) * 100 # Convert ADC value to percentage (0-100%)
# Print the sensor readings in Wokwi Serial Monitor
print('Temperature: {:.2f}°C'.format(temp))
print('pH Level: {:.2f}'.format(ph))
print('Water Clarity (Turbidity): {:.2f}%'.format(turbidity))
print('---')
# Wait for a short period before taking another reading
time.sleep(5)