You are on page 1of 10

6B.

PROGRAM:
import RPi.GPIO as GPIO
import time

# Set the GPIO mode


GPIO.setmode(GPIO.BCM)

# Set up the GPIO pin for output


led_pin = 17
GPIO.setup(led_pin, GPIO.OUT)

try:
while True:
# Turn the LED on
GPIO.output(led_pin, GPIO.HIGH)
print("LED on")
time.sleep(1) # Wait for 1 second

# Turn the LED off


GPIO.output(led_pin, GPIO.LOW)
print("LED off")
time.sleep(1) # Wait for 1 second

except KeyboardInterrupt:
# Cleanup GPIO settings
GPIO.cleanup()

Output:

7a.
PROGRAM
#include <DHT.h>

#define DHTPIN 2 // Pin connected to the DHT sensor


#define DHTTYPE DHT11 // Change to DHT22 if you're using that sensor

DHT dht(DHTPIN, DHTTYPE);


void setup() {
Serial.begin(9600);
Serial.println("DHT sensor test:");
dht.begin();
}

void loop() {
delay(2000); // Wait for 2 seconds between measurements

float humidity = dht.readHumidity();


float temperature = dht.readTemperature();

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

import RPi.GPIO as GPIO


import time

# 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)

# Set Trigger after 0.01ms to LOW


time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)

StartTime = time.time()
StopTime = time.time()

# Save StartTime
while GPIO.input(GPIO_ECHO) == 0:
StartTime = time.time()

# Save time of arrival


while GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time()

# Time difference between start and arrival


TimeElapsed = StopTime - StartTime
# Multiply

OUTPUT:

8a.
PROGRAM
Transmitter code:
#include <DHT.h>
#include <SoftwareSerial.h>

#define DHTPIN 2 // Pin connected to the DHT sensor


#define DHTTYPE DHT11 // Change to DHT22 if you're using that sensor

#define bluetoothTx 3
#define bluetoothRx 4

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);


DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
bluetooth.begin(9600);
dht.begin();
}

void loop() {
delay(2000); // Wait for 2 seconds between readings

float humidity = dht.readHumidity();


float temperature = dht.readTemperature();

if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

// Send data via Bluetooth


bluetooth.print("H:");
bluetooth.print(humidity);
bluetooth.print(",T:");
bluetooth.println(temperature);
}

Receiver Code:

#include <SevSeg.h>
#include <SoftwareSerial.h>

#define bluetoothTx 2
#define bluetoothRx 3

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

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:

import RPi.GPIO as GPIO


import time

# 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:

import RPi.GPIO as GPIO


import time

# 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:

import RPi.GPIO as GPIO


import time

# 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:

import RPi.GPIO as GPIO


import time

# 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

You might also like