0% found this document useful (0 votes)
64 views10 pages

Raspberry Pi GPIO Programming Examples

The document contains code snippets for Arduino programs that perform basic tasks like blinking an LED, reading from a sensor, and transmitting sensor data wirelessly. The programs demonstrate using GPIO pins and libraries on Raspberry Pi and Arduino boards.
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)
64 views10 pages

Raspberry Pi GPIO Programming Examples

The document contains code snippets for Arduino programs that perform basic tasks like blinking an LED, reading from a sensor, and transmitting sensor data wirelessly. The programs demonstrate using GPIO pins and libraries on Raspberry Pi and Arduino boards.
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

6B.

PROGRAM:
import [Link] as GPIO
import time

# Set the GPIO mode


[Link]([Link])

# Set up the GPIO pin for output


led_pin = 17
[Link](led_pin, [Link])

try:
while True:
# Turn the LED on
[Link](led_pin, [Link])
print("LED on")
[Link](1) # Wait for 1 second

# Turn the LED off


[Link](led_pin, [Link])
print("LED off")
[Link](1) # Wait for 1 second

except KeyboardInterrupt:
# Cleanup GPIO settings
[Link]()

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() {
[Link](9600);
[Link]("DHT sensor test:");
[Link]();
}

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

float humidity = [Link]();


float temperature = [Link]();

if (isnan(humidity) || isnan(temperature)) {
[Link]("Failed to read from DHT sensor!");
return;
}

[Link]("Humidity: ");
[Link](humidity);
[Link](" %\t");
[Link]("Temperature: ");
[Link](temperature);
[Link](" °C");
}

OUTPUT:

7b.
PROGRAM

import [Link] as GPIO


import time

# GPIO Pins
GPIO_TRIGGER = 17
GPIO_ECHO = 18

# Setup GPIO
[Link]([Link])
[Link](GPIO_TRIGGER, [Link])
[Link](GPIO_ECHO, [Link])

def distance():
# Set Trigger to HIGH
[Link](GPIO_TRIGGER, True)

# Set Trigger after 0.01ms to LOW


[Link](0.00001)
[Link](GPIO_TRIGGER, False)

StartTime = [Link]()
StopTime = [Link]()

# Save StartTime
while [Link](GPIO_ECHO) == 0:
StartTime = [Link]()

# Save time of arrival


while [Link](GPIO_ECHO) == 1:
StopTime = [Link]()

# 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() {
[Link](9600);
[Link](9600);
[Link]();
}

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

float humidity = [Link]();


float temperature = [Link]();

if (isnan(humidity) || isnan(temperature)) {
[Link]("Failed to read from DHT sensor!");
return;
}

// Send data via Bluetooth


[Link]("H:");
[Link](humidity);
[Link](",T:");
[Link](temperature);
}

Receiver Code:

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

#define bluetoothTx 2
#define bluetoothRx 3

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

SevSeg sevseg;

void setup() {
[Link](9600);
[Link](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;
[Link](hardwareConfig, numDigits, digitPins, segmentPins,
resistorsOnSegments);
[Link](90);
}

void loop() {
if ([Link]()) {
char receivedChar = (char)[Link]();

if (receivedChar == 'H') {
String humidity = readSensorData();
[Link](atof(humidity.c_str()), 0);
[Link]();
}
}
}

String readSensorData() {
String data = "";
while ([Link]()) {
char c = [Link]();
if (c == ',') break; // Stop reading if comma is encountered
data += c;
}
return data;
}

OUTPUT:

8B.
PROGRAM:

import [Link] as GPIO


import time

# Set up GPIO
[Link]([Link])
button_pin = 11 # Example GPIO pin, change as per your setup
[Link](button_pin, [Link], pull_up_down=GPIO.PUD_DOWN)

# Main loop
try:
while True:
if [Link](button_pin) == [Link]: # 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)
[Link](0.5) # Debounce time, adjust as needed
finally:
[Link]() # Clean up GPIO on exit

RECEIVER:

import [Link] as GPIO


import time

# Set up GPIO
[Link]([Link])
led_pin = 12 # Example GPIO pin, change as per your setup
[Link](led_pin, [Link])

# 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...")
[Link](led_pin, [Link])
[Link](0.5) # Blink duration
[Link](led_pin, [Link])
[Link](0.5) # Adjust as needed
finally:
[Link]() # Clean up GPIO on exit

TX OUTPUT:
RX OUTPUT:

9. B
PROGRAM

Transmitter:

import [Link] as GPIO


import time

# Set up GPIO
[Link]([Link])
button_pin = 11 # Example GPIO pin, change as per your setup
[Link](button_pin, [Link], pull_up_down=GPIO.PUD_DOWN)

# Main loop
try:
while True:
if [Link](button_pin) == [Link]: # 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: [Link](transmit_pin, [Link])
[Link](0.5) # Debounce time, adjust as needed
finally:
[Link]() # Clean up GPIO on exit
Receiver:

import [Link] as GPIO


import time

# Set up GPIO
[Link]([Link])
buzzer_pin = 12 # Example GPIO pin, change as per your setup
[Link](buzzer_pin, [Link])

# 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...")
[Link](buzzer_pin, [Link])
[Link](1) # Buzzer activation duration
[Link](buzzer_pin, [Link])
[Link](1) # Delay between activations
finally:
[Link]() # Clean up GPIO on exit

TX OUTPUT:
RX OUTPUT:

4
PROGRAM:

void setup() {
[Link](115200);
// Print Hello,world
[Link]("Hello,world!");
// Add two numbers
float a = 1.2;
float b = 5.3;
float sum = a + b;
[Link]("a = "); [Link](a);
[Link]("b = "); [Link](b);
[Link]("Sum = "); [Link](sum);
}

void loop() {
// empty
delay(10);
}

Simulated output

You might also like