0% found this document useful (0 votes)
2 views4 pages

Thesis Code

The document contains a Python script for a Raspberry Pi that implements a tachometer using an IR sensor and an LCD display, along with a relay control system. The script initializes GPIO pins, calculates RPM based on sensor input, and updates the LCD display accordingly. It also includes a switch to control a relay, turning it on or off based on the switch state, with appropriate error handling and cleanup procedures.

Uploaded by

Kairu-kun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Thesis Code

The document contains a Python script for a Raspberry Pi that implements a tachometer using an IR sensor and an LCD display, along with a relay control system. The script initializes GPIO pins, calculates RPM based on sensor input, and updates the LCD display accordingly. It also includes a switch to control a relay, turning it on or off based on the switch state, with appropriate error handling and cleanup procedures.

Uploaded by

Kairu-kun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

import RPi.

GPIO as GPIO

import time

from rpi_lcd import LCD

# --- Constants ---

IR_SENSOR_PIN = 17 # GPIO pin for the IR sensor

I2C_ADDRESS = 0x27 # I2C address for the LCD

DEBOUNCE_TIME = 0.005 # 10 ms debounce time to filter out noise

# --- Initialization ---

GPIO.setmode(GPIO.BCM)

GPIO.setup(IR_SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)

lcd = None

try:

lcd = LCD(address=I2C_ADDRESS)

except Exception as e:

print(f"Error initializing LCD: {e}")

# --- Tachometer Variables ---

count = 0 # Initialize count

rpm = 0

last_pulse_time = time.time() # Track the last pulse time for debounce logic

start_time = last_pulse_time # Timer for RPM calculation

# --- Functions ---

def calculate_rpm():

global count, start_time, rpm

elapsed_time = time.time() - start_time

if elapsed_time >= 0.5: if elapsed_time > 0:

rpm = (count / elapsed_time) * 60 * 2

else:

rpm = 0

count = 0
start_time = time.time()

def display_rpm():

if lcd:

try:

lcd.text(f"RPM: {int(rpm)}", 1)

lcd.text(" ", 2)

except Exception as e:

print(f"Error updating LCD: {e}")

def main():

global count, last_pulse_time

print("Tachometer Program Started")

try:

while True:

# Check if sensor is triggered (LOW state)

if GPIO.input(IR_SENSOR_PIN) == GPIO.LOW: # Triggered by sensor (LOW state)

current_time = time.time()

# Debounce logic

if current_time - last_pulse_time > DEBOUNCE_TIME:

count += 1

last_pulse_time = current_time

calculate_rpm() # Update RPM

display_rpm() # Update LCD display

time.sleep(0.0005) # Short delay for smoother updates

except KeyboardInterrupt:

print("Program interrupted by user.")

except Exception as e:

print(f"Unexpected error: {e}")

finally:

# Cleanup GPIO and LCD

if lcd:
try:

lcd.clear()

except Exception as e:

print(f"Error clearing LCD: {e}")

GPIO.cleanup()

print("GPIO cleanup completed. Exiting program.")

if __name__ == "__main__":

main()

import RPi.GPIO as GPIO

import time

# Pin configuration

SWITCH_PIN = 12 # GPIO pin connected to the switch

RELAY_PIN = 16 # GPIO pin connected to the relay

# GPIO setup

GPIO.setmode(GPIO.BCM) # Use BCM pin numbering

GPIO.setup(SWITCH_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Switch as input with pull-down


resistor

GPIO.setup(RELAY_PIN, GPIO.OUT) # Relay as output


try:

while True:

if GPIO.input(SWITCH_PIN) == GPIO.HIGH: # Check if the switch is ON

GPIO.output(RELAY_PIN, GPIO.HIGH) # Turn ON the relay

print("Relay ON")

else:

GPIO.output(RELAY_PIN, GPIO.LOW) # Turn OFF the relay

print("Relay OFF")

time.sleep(0.1) # Small delay to avoid CPU overuse

except KeyboardInterrupt:

print("Exiting program...")

finally:

GPIO.cleanup() # Reset GPIO settings

You might also like