top of page

Wink It Blink It

#CODE

#importing Libraries
from machine import Pin
import time
#Creating variable and setting Pin number 25 for output
led=Pin(25,Pin.OUT)
while True:
    #Led ON (True)
     led.value(1)
     time.sleep(1)
    #Led OFF (False)
     led.value(0)
     time.sleep(1)

Sweat or Sweater

#CODE

import machine
import dht
import time
# Initialize the DHT11 sensor on GPIO17
dht_sensor = dht.DHT11(machine.Pin(17))
def read_dht11():
    dht_sensor.measure()
    # Perform a measurement
    temperature = dht_sensor.temperature()
    # Read temperature
    humidity = dht_sensor.humidity()
    # Read humidity
    return temperature, humidity
while True:
    try:
        temp, hum = read_dht11()
        print(f"Temperature: {temp}°C, Humidity: {hum}%")
    except Exception as e:
        print(f"Error reading data: {e}")
# Delay for a few seconds before the next reading
time.sleep(2) # Sleep for 2 seconds

Touch me not

#CODE

from machine import Pin
from time import sleep

# Define GPIO pins
BUZZ_PIN = 20 # Change this to the GPIO pin connected to the LED
TOUCH_PIN = 0  # Change this to the GPIO pin connected to the touch sensor

# Initialize LED pin
buzz = Pin(BUZZ_PIN, Pin.OUT)

# Initialize touch sensor pin
touch = machine.Pin(TOUCH_PIN, machine.Pin.IN)

while True:
    # Check if the touch sensor is pressed
    if touch.value() == 1:
        # If pressed, turn on the Bell
        buzz.value(1)
        print("Touch sensor pressed")
    else:
        # If not pressed, turn off the Bell
        buzz.value(0)
    
    # Add a small delay to debounce the touch sensor
    sleep(1)

Just an Inch away !

#CODE

import machine
import time

#Define GPIO pins
TRIG_PIN = machine.Pin(17, machine.Pin.OUT)
ECHO_PIN = machine.Pin(18, machine.Pin.IN)

# Function to measure distance
def measure_distance():
    TRIG_PIN.value(1)
    time.sleep_us(1)
    TRIG_PIN.value(0)
    while ECHO_PIN.value() == 0:
        pulse_start = time.ticks_us()
    while ECHO_PIN.value() == 1:
        pulse_end = time.ticks_us()
    pulse_duration = pulse_end - pulse_start
    distance_cm = (pulse_duration / 2) / 29.1
    # Speed of sound is approximately 343 m/s
    return distance_cm
try:
    while True:
        distance = measure_distance()
        print("Distance: {:.2f} cm".format(distance))
        time.sleep(2)
except KeyboardInterrupt:
    pass

Tank calling

#CODE

import machine
import time

#Define GPIO pins
TRIG_PIN = machine.Pin(17, machine.Pin.OUT)
ECHO_PIN = machine.Pin(18, machine.Pin.IN)
BUZZER_PIN = machine.Pin(16, machine.Pin.OUT)

# Function to measure distance
def measure_distance():
    TRIG_PIN.value(1)
    time.sleep_us(1)
    TRIG_PIN.value(0)
    while ECHO_PIN.value() == 0:
        pulse_start = time.ticks_us()
    while ECHO_PIN.value() == 1:
        pulse_end = time.ticks_us()
    pulse_duration = pulse_end - pulse_start
    distance_cm = (pulse_duration / 2) / 29.1
    # Speed of sound is approximately 343 m/s
    return distance_cm
try:
    while True:
        distance = measure_distance()
        print("Distance: {:.2f} cm".format(distance))

        if distance < 10:
            BUZZER_PIN.value(1) # Turn on the buzzer
        else:
            BUZZER_PIN.value(0) # Turn off the buzzer
        time.sleep(1)

except KeyboardInterrupt:
    pass

Thirsty Soil

#Code to find sensors minimum and maximum range

import machine
import time
adc = machine.ADC(26) # Use GP26 as the ADC pin
while True:
    moisture_value = adc.read_u16()
    # Read analog value
    # Truncate to 12 bits
    moisture_value = moisture_value & 0xFFF
    print("Moisture Value: {}".format(moisture_value))
    time.sleep(1) # Delay for better readability

NOTE:-

First, record the sensor value when it's in AIR (dry conditions), thennote its value when submerged in WATER to establish the raw values.

#Code to find Humidity in Soil

import machine
import time

# Define your own calibration values
SENSOR_MIN = 41300 # Raw value in air or dry soil
SENSOR_MAX = 16000 # Raw value in water or saturated soil
adc = machine.ADC(26) # Use GP26 as the ADC pin

while True:
    raw_value = adc.read_u16() # Read raw analog value
    print("raw_value= ",raw_value)
    # Map the raw value to a moisture range (0-100%)
    moisture_value = (raw_value - SENSOR_MIN) / (SENSOR_MAX - SENSOR_MIN) * 100

    print("Moisture Value: {:.2f}%".format(moisture_value))
    time.sleep(1) # Delay for better readability

Tick Tick go!

#CODE

import tm1637
from machine import Pin
from time import sleep, localtime

# Initialize the TM1637 display object
mydisplay =tm1637.TM1637(clk=Pin(0), dio=Pin(1))

# Set initial time
current_time = (12, 0) # Initial time (12:00)

def display_time(time):
    mydisplay.numbers(time[0], time[1])

def main():
    while True:
        current_time = localtime()[3:5]
        display_time(current_time)
        sleep(1)

if __name__ == "__main__":
    main()

Follow the lights

#CODE

#importing Libraries
from machine import Pin
import tm1637
from time import sleep
import sys

#setting pin numbers to led and display
red=Pin(2,Pin.OUT)
yellow=Pin(3,Pin.OUT)
green=Pin(4,Pin.OUT)
mydisplay = tm1637.TM1637(clk=Pin(0), dio=Pin(1))

#Menu card
print("Enter 'R' for Red light ")
print("Enter 'Y' for Yellow light ")
print("Enter 'G' for Green light ")

#input from user
x=input("Enter light code to turn on:- ")

#checking condition on the basis of input
if x=='R':
    red.value(1)
    yellow.value(0)
    green.value(0)
elif x=='Y':
    red.value(0)
    yellow.value(1)
    green.value(0)
elif x=='G':
    red.value(0)
    yellow.value(0)
    green.value(1)
else:
    print("Invalid code")
    sys.exit()

​

#infinite loop
while True:
    #when Red light is active
    if red.value()==1:
        for i in range(30,0,-1):
            mydisplay.number(i)
            sleep(1)
        red.value(0)
        yellow.value(1)
        for i in range(5,0,-1):
            mydisplay.number(i)
            sleep(1)
        yellow.value(0)
        green.value(1)
    #When yellow light is active
    elif yellow.value()==1:
        for i in range(5,0,-1):
            mydisplay.number(i)
            sleep(1)
        yellow.value(0)
        green.value(1)
        for i in range(30,0,-1):
            mydisplay.number(i)
            sleep(1)
        green.value(0)
        red.value(1)
    #when green light is active
    elif green.value()==1:
        for i in range(30,0,-1):
            mydisplay.number(i)
            sleep(1)
        green.value(0)
        yellow.value(1)
        for i in range(5,0,-1):
            mydisplay.number(i)
            sleep(1)
        yellow.value(0)
        red.value(1)

Watch out

#CODE

import machine
import time

# Create an ADC object for the LDR pin
adc = machine.ADC(26) # Use GP26 as the ADC pin
buzzer_pin = machine.Pin(3, machine.Pin.OUT)

while True:
    ldr_value = adc.read_u16() # Read the analog value

    # Truncate to 12 bits
    ldr_value = ldr_value & 0xFFF

    # Print the LDR value
    print("LDR Value:", ldr_value)

    time.sleep(1) # Delay for reading stability

    if(ldr_value>500):
        buzzer_pin.on() # Turn the buzzer on
        print("Buzzer ON")

        time.sleep(1)

        buzzer_pin.off() # Turn the buzzer off
        print("theif")
    else:
        buzzer_pin.off()

Automate your Home

#CODE

import machine
import time

# Define GPIO pins
TRIG_PIN = machine.Pin(17, machine.Pin.OUT)
ECHO_PIN = machine.Pin(18, machine.Pin.IN)
BUZZER_PIN = machine.Pin(0, machine.Pin.OUT)
LED_PIN = machine.Pin(3, machine.Pin.OUT)
# Create an ADC object for the LDR pin
adc = machine.ADC(26) # Use GP26 as the ADC pin

LED_PIN.value(0)
BUZZER_PIN.value(0)
count=0
Count=0

 

# Function to measure distance
def measure_distance():
    TRIG_PIN.value(1)
    time.sleep_us(1)
    TRIG_PIN.value(0)

    while ECHO_PIN.value() == 0:
        pulse_start = time.ticks_us()
    while ECHO_PIN.value() == 1:
        pulse_end = time.ticks_us()

    pulse_duration = pulse_end - pulse_start
    distance_cm = (pulse_duration / 2) / 29.1
    # Speed of sound is approximately 343 m/s
    return distance_cm

# Function to handle automatic doorbell
def doorbell_task():
    global Count
        while True:
            light_control_task()
            distance = measure_distance()
            print("Distance: {:.2f} cm".format(distance))
            if distance < 10:
                for i in range(10):
                    light_control_task()
                    distance = measure_distance()
                    Count += 1
                    print("Distance: {:.2f} cm".format(distance))

                    if distance < 10:
                        pass
                    else:
                        Count = 0
                        break
                    if Count == 10:
                        BUZZER_PIN.value(1) # Turn on the buzzer
                        for j in range(5):
                            light_control_task()
                            print("Bell")
                            time.sleep(1)
                        BUZZER_PIN.value(0) # Turn off the buzzer
                    time.sleep(1)
                time.sleep(1)

​

# Function to handle automatic light control
def light_control_task():

    global count
        ldr_value = adc.read_u16() # Read the analog value
        # Truncate to 12 bits
        ldr_value = ldr_value & 0xFFF
        # Print the LDR value
        print("LDR Value:", ldr_value)
        time.sleep(1) # Delay for reading stability

        if ldr_value > 1000 and count == 0:
            LED_PIN.value(1) # Turn the LED on
            count = 1

        elif ldr_value > 1000 and count == 1:
            LED_PIN.value(0) # Turn the LED off
            count = 0

# Start the main loop to simulate cooperative multitasking
try:
    while True:
        doorbell_task()
except KeyboardInterrupt:
    pass

bottom of page