Finger Detection Heartbeat

🎮 Display the Raw Analog Signal

Goal 

Read the sensor at regular intervals and display the raw analog signal on the PC. 

Concepts 

Analog Input · ADC · Raw Signal · Loop · Serial Output

import machine  # Controls the board hardware
import time     # Controls pauses and timing

# Sets up GPIO2 as an analog input for the optical sensor
# GPIO2 corresponds to A0 on the Sensor Shield V5.0
adc2 = machine.ADC(machine.Pin(2))

adc2.atten(machine.ADC.ATTN_11DB)  # Sets the analog input range

while True:  # Repeats forever

    signal = adc2.read_u16()  # Reads the raw sensor signal

    print(signal)  # Sends the raw value to the PC

    time.sleep(0.1)  # Waits before reading again

🎮 Observe the Signal with a Finger

Goal 

Observe how the analog signal changes when a finger is placed correctly on the sensor. 

Concepts 

Analog Input · ADC · Signal Change · Observation · Serial Output

import machine  # Controls the board hardware
import time     # Controls pauses and timing

# Sets up GPIO2 as an analog input for the optical sensor
# GPIO2 corresponds to A0 on the Sensor Shield V5.0
adc2 = machine.ADC(machine.Pin(2))

adc2.atten(machine.ADC.ATTN_11DB)  # Sets the analog input range

while True:  # Repeats forever

    signal = adc2.read_u16()  # Reads the optical signal

    print('Signal:', signal)  # Shows the signal on the PC

    time.sleep(0.1)  # Waits before reading again

🎮 Detect Noticeable Peaks

Goal 

Experiment with detecting noticeable peaks when the signal rises above a chosen threshold. 

Concepts 

Analog Input · Threshold · Comparison · Peak Detection · Serial Output

import machine  # Controls the board hardware
import time     # Controls pauses and timing

# Sets up GPIO2 as an analog input for the optical sensor
# GPIO2 corresponds to A0 on the Sensor Shield V5.0
adc2 = machine.ADC(machine.Pin(2))

adc2.atten(machine.ADC.ATTN_11DB)  # Sets the analog input range

threshold = 35000  # Sets an experimental peak threshold

last_signal = adc2.read_u16()  # Saves the first reading

while True:  # Repeats forever

    signal = adc2.read_u16()  # Reads the current signal

    print('Signal:', signal)  # Shows the current value

    if signal >= threshold and last_signal < threshold:  # Checks for a new peak

        print('Peak detected')  # Reports the peak to the PC

    last_signal = signal  # Saves the current reading

    time.sleep(0.05)  # Waits before reading again

🎮 Calculate the Average Signal

Goal 

Calculate an average from several sensor readings and compare the current reading with the average. 

Concepts 

Analog Input · Average · Loop · Comparison · Serial Output

import machine  # Controls the board hardware
import time     # Controls pauses and timing

# Sets up GPIO2 as an analog input for the optical sensor
# GPIO2 corresponds to A0 on the Sensor Shield V5.0
adc2 = machine.ADC(machine.Pin(2))

adc2.atten(machine.ADC.ATTN_11DB)  # Sets the analog input range

while True:  # Repeats forever

    total = 0  # Starts the total at zero

    for _count in range(10):  # Takes 10 readings

        signal = adc2.read_u16()  # Reads the optical signal

        total += signal  # Adds the reading to the total

        time.sleep(0.05)  # Waits before the next reading

    average = total // 10  # Calculates the average

    signal = adc2.read_u16()  # Reads the current signal

    print('Average:', average)  # Shows the average

    print('Current:', signal)  # Shows the current reading

    if signal > average:  # Checks if the signal is above average

        print('Above average')  # Reports the comparison

    else:  # Runs when the signal is not above average

        print('Below average')  # Reports the comparison

    time.sleep(0.2)  # Waits before repeating

🎮 Optical Signal Monitor

Goal 

Create a Serial signal monitor that reports the raw reading, average signal, and noticeable peaks. 

Concepts 

Analog Input · Average · Threshold · Comparison · Peak Detection · Serial Output

import machine  # Controls the board hardware
import time     # Controls pauses and timing

# Sets up GPIO2 as an analog input for the optical sensor
# GPIO2 corresponds to A0 on the Sensor Shield V5.0
adc2 = machine.ADC(machine.Pin(2))

adc2.atten(machine.ADC.ATTN_11DB)  # Sets the analog input range

while True:  # Repeats forever

    total = 0  # Starts the total at zero

    for _count in range(10):  # Takes 10 readings

        signal = adc2.read_u16()  # Reads the optical signal

        total += signal  # Adds the reading to the total

        time.sleep(0.05)  # Waits before the next reading

    average = total // 10  # Calculates the average

    signal = adc2.read_u16()  # Reads the current signal

    difference = signal - average  # Finds the difference from average

    print('Raw:', signal)  # Shows the current signal

    print('Average:', average)  # Shows the average signal

    if difference > 3000:  # Checks for a noticeable rise

        print('Peak detected')  # Reports a possible signal peak

    else:  # Runs when no large rise is detected

        print('No peak')  # Reports no peak

    print('---')  # Separates each report

    time.sleep(0.1)  # Waits before repeating