Final Program
Final Program
PROGRAM:
import RPi.GPIO as GPIO
import time
try:
while True:
# Turn the LED on
GPIO.output(led_pin, GPIO.HIGH)
print("LED on")
time.sleep(1) # Wait for 1 second
except KeyboardInterrupt:
# Cleanup GPIO settings
GPIO.cleanup()
Output:
7a.
PROGRAM
#include <DHT.h>
void loop() {
delay(2000); // Wait for 2 seconds between measurements
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
}
OUTPUT:
7b.
PROGRAM
# GPIO Pins
GPIO_TRIGGER = 17
GPIO_ECHO = 18
# Setup GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
def distance():
# Set Trigger to HIGH
GPIO.output(GPIO_TRIGGER, True)
StartTime = time.time()
StopTime = time.time()
# Save StartTime
while GPIO.input(GPIO_ECHO) == 0:
StartTime = time.time()
OUTPUT:
8a.
PROGRAM
Transmitter code:
#include <DHT.h>
#include <SoftwareSerial.h>
#define bluetoothTx 3
#define bluetoothRx 4
void setup() {
Serial.begin(9600);
bluetooth.begin(9600);
dht.begin();
}
void loop() {
delay(2000); // Wait for 2 seconds between readings
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Receiver Code:
#include <SevSeg.h>
#include <SoftwareSerial.h>
#define bluetoothTx 2
#define bluetoothRx 3
SevSeg sevseg;
void setup() {
Serial.begin(9600);
bluetooth.begin(9600);
byte numDigits = 4;
byte digitPins[] = {4, 5, 6, 7};
byte segmentPins[] = {8, 9, 10, 11, 12, 13, A0, A1};
bool resistorsOnSegments = false;
bool updateWithDelays = false;
byte hardwareConfig = COMMON_CATHODE;
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins,
resistorsOnSegments);
sevseg.setBrightness(90);
}
void loop() {
if (bluetooth.available()) {
char receivedChar = (char)bluetooth.read();
if (receivedChar == 'H') {
String humidity = readSensorData();
sevseg.setNumber(atof(humidity.c_str()), 0);
sevseg.refreshDisplay();
}
}
}
String readSensorData() {
String data = "";
while (bluetooth.available()) {
char c = bluetooth.read();
if (c == ',') break; // Stop reading if comma is encountered
data += c;
}
return data;
}
OUTPUT:
8B.
PROGRAM:
# Set up GPIO
GPIO.setmode(GPIO.BOARD)
button_pin = 11 # Example GPIO pin, change as per your setup
GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# Main loop
try:
while True:
if GPIO.input(button_pin) == GPIO.HIGH: # Assuming button connected
between pin and GND
print("Button clicked! Sending signal to receiving Pi...")
# Code to transmit signal to receiving Pi (e.g., via GPIO, WiFi, Bluetooth)
time.sleep(0.5) # Debounce time, adjust as needed
finally:
GPIO.cleanup() # Clean up GPIO on exit
RECEIVER:
# Set up GPIO
GPIO.setmode(GPIO.BOARD)
led_pin = 12 # Example GPIO pin, change as per your setup
GPIO.setup(led_pin, GPIO.OUT)
# Main loop
try:
while True:
# Code to receive signal from transmitting Pi (e.g., via GPIO, WiFi, Bluetooth)
# For simplicity, let's just assume a signal is received and blink the LED
print("Signal received! Blinking LED...")
GPIO.output(led_pin, GPIO.HIGH)
time.sleep(0.5) # Blink duration
GPIO.output(led_pin, GPIO.LOW)
time.sleep(0.5) # Adjust as needed
finally:
GPIO.cleanup() # Clean up GPIO on exit
TX OUTPUT:
RX OUTPUT:
9. B
PROGRAM
Transmitter:
# Set up GPIO
GPIO.setmode(GPIO.BOARD)
button_pin = 11 # Example GPIO pin, change as per your setup
GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# Main loop
try:
while True:
if GPIO.input(button_pin) == GPIO.HIGH: # Assuming button connected
between pin and GND
print("Button clicked! Sending signal to receiving Pi...")
# Code to transmit signal to receiving Pi (e.g., via GPIO, WiFi, Bluetooth)
# For GPIO example, you can use a similar approach as in the receiving Pi
# Example: GPIO.output(transmit_pin, GPIO.HIGH)
time.sleep(0.5) # Debounce time, adjust as needed
finally:
GPIO.cleanup() # Clean up GPIO on exit
Receiver:
# Set up GPIO
GPIO.setmode(GPIO.BOARD)
buzzer_pin = 12 # Example GPIO pin, change as per your setup
GPIO.setup(buzzer_pin, GPIO.OUT)
# Main loop
try:
while True:
# Code to receive signal from transmitting Pi (e.g., via GPIO, WiFi, Bluetooth)
# For simplicity, let's just assume a signal is received and activate the buzzer
print("Signal received! Activating buzzer...")
GPIO.output(buzzer_pin, GPIO.HIGH)
time.sleep(1) # Buzzer activation duration
GPIO.output(buzzer_pin, GPIO.LOW)
time.sleep(1) # Delay between activations
finally:
GPIO.cleanup() # Clean up GPIO on exit
TX OUTPUT:
RX OUTPUT:
4
PROGRAM:
void setup() {
Serial1.begin(115200);
// Print Hello,world
Serial1.println("Hello,world!");
// Add two numbers
float a = 1.2;
float b = 5.3;
float sum = a + b;
Serial1.print("a = "); Serial1.println(a);
Serial1.print("b = "); Serial1.println(b);
Serial1.print("Sum = "); Serial1.println(sum);
}
void loop() {
// empty
delay(10);
}
Simulated output