Analog Sensors.  Hall, Moisture, Temperature and a Pico





The Picture shows 4 sensors

and a magnet.
There is a 5th sensor, temperature, on the Pico board.

  1. A capacitive moisture sensor
  2. A digital hall effect sensor 0/1 (44E left)
  3. An analog hall effect sensor - 0-3.3V (49E right)
  4. A push-button switch


Thonny

Pico Python Code




# Get the libraries we need
from machine import Pin, Timer, I2C
from ssd1306 import SSD1306_I2C
import utime

# hall d count global
dcount = 0

# Blink Speeds
blinkRate1 = .5  # seconds on/off
blinkRate2 = 2

# hall effect sensor
hall_val = machine.ADC(28)

# I2C
i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000)
# oled (width, height, i2c)
oled = SSD1306_I2C(128, 64, i2c)
# moisture sensor
moisture_val = machine.ADC(27)

# Temperature Sensor and Constants
sensor_temp = machine.ADC(4)		# Temperature is on ADC 4
conversion_factor = 3.3 / (65535)   # 3.3 V maximum / 16 bits.

# Set up the I/O pins
# Leds
led1 = Pin(25, Pin.OUT)
led2 = Pin(15, Pin.OUT)

# Push button
button = Pin(14, Pin.IN, Pin.PULL_DOWN)

#hall digital
hall_dval = Pin(22, Pin.IN)

# Instantiate a couple of timers for the leds
timer1 = Timer()
timer2 = Timer()

# Functions called when a timer expires
def blink1(timer1):
    led1.toggle()
    
def blink2(timer2):
    led2.toggle()
    
# Function called when halld is triggered
def countd(pin):
    global dcount
    dcount +=1

# link the interrupt for the halld
hall_dval.irq(trigger=machine.Pin.IRQ_FALLING, handler=countd)

# Start the timers
timer1.init(freq=1/blinkRate1, mode=Timer.PERIODIC, callback=blink1)
timer2.init(freq=1/blinkRate2, mode=Timer.PERIODIC, callback=blink2)

i = 0
# Loop forever clswhecking the button, counting and displaying
while 1:
    
    # if button is pushed swap the blink rates and restart the timers
    if button.value():
        blinkRate1, blinkRate2 = blinkRate2, blinkRate1
        timer1.init(freq=1/blinkRate1, mode=Timer.PERIODIC, callback=blink1)
        timer2.init(freq=1/blinkRate2, mode=Timer.PERIODIC, callback=blink2)
        print ("button")
    
    # Get the temperature
    reading = sensor_temp.read_u16() * conversion_factor
    
    # Get the hall value and
    # convert to -10 to +10 for max north south or south north - who knows?
    hallReading = ((hall_val.read_u16() - 32767) / 3277) * -1
    
    # Get the digital hall value 44E Chip
    halldReading = not hall_dval.value()
    
    # Get the moisture value
    moistureReading = (moisture_val.read_u16())
    
    # 60000 = 0 inches, 27000 = 2.5 inches inserted in water 
    inches = 2.5 - ((moistureReading - 27000) * 2.5/33000)
    
    # The temperature sensor measures the Vbe voltage of a biased 
# bipolar diode, connected to the fifth ADC channel # Typically, Vbe = 0.706V at 27 degrees C, with a slope
# of -1.721mV (0.001721) per degree. temperature = 27 - (reading - 0.706)/0.001721 # Display count, temperatures and moisture. # Display count, temperatures and moisture. print ("\nCount: %5d" %(i)) print ("Temperature: %3.3f" %(temperature)) print ("Hall: %5.2f" %(hallReading)) print ("HallD: ", end='') print (dcount) print ("Depth: %5.2f" %(inches)) # Display on the oled oled.fill(0) oled.show() oled.text("Anal, Dig, Depth",0,0) oled.text(str(round(hallReading,2)),0,12) oled.text(str(halldReading),0,24) oled.text(str(round(inches,2)), 0,36) oled.text(str(dcount),0,50) oled.show() i += 1 utime.sleep(2);

What are hall effect sensors good for?

More Information: