5. INPUT & OUTPUT¶
in this week we are learning about input & output device and learn the defferent type of them and how you can use then
Research¶
I/O (input/output), pronounced “eye-oh,” describes any operation, program or device that transfers data to or from a computer. Common I/O devices include printers, hard disks, keyboards and mice
Useful links¶
- Google it will provide a big help with understanding code writing
- ChatGPT it can give you a very good code example
Code Example¶
OUTPUT¶
this code is about usinsg the HORN as an out put to make twinkle twinkle tone
from machine import Pin, PWM
import time
# Define the pin connected to the buzzer
buzzer_pin = 18
buzzer = PWM(Pin(buzzer_pin))
# Note frequencies (in Hz)
notes = {
'C': 261, 'D': 293, 'E': 329, 'F': 349, 'G': 392,
'A': 440, 'B': 493, 'C5': 523, 'D5': 587, 'E5': 659,
'F5': 698, 'G5': 784
}
# Twinkle Twinkle Little Star melody
melody = [
('C', 400), ('C', 400), ('G', 400), ('G', 400),
('A', 400), ('A', 400), ('G', 800), ('F', 400),
('F', 400), ('E', 400), ('E', 400), ('D', 400),
('D', 400), ('C', 800), ('G', 400), ('G', 400),
('F', 400), ('F', 400), ('E', 400), ('E', 400),
('D', 800), ('G', 400), ('G', 400), ('F', 400),
('F', 400), ('E', 400), ('E', 400), ('D', 800),
('C', 400), ('C', 400), ('G', 400), ('G', 400),
('A', 400), ('A', 400), ('G', 800), ('F', 400),
('F', 400), ('E', 400), ('E', 400), ('D', 400),
('D', 400), ('C', 800)
]
after that I start to test the RGB LED and tryied to do some code expermint with it with this one we need to use some spisific liberary
from machine import Pin
import neopixel
import time
# Define the GPIO pin connected to the LED strip
data_pin = 18
num_pixels = 4 # Number of LEDs in the strip
# Initialize the NeoPixel strip
strip = neopixel.NeoPixel(Pin(data_pin), num_pixels)
# Define static colors for each pixel
# You can customize these colors
colors = [
(255, 0, 0), # Red
(0, 255, 0), # Green
(0, 0, 255), # Blue
(255, 255, 0), # Yellow
(0, 255, 255), # Cyan
(255, 0, 255) # Magenta
]
def display_static_colors():
"""Set each pixel to a static color."""
for i in range(num_pixels):
color_index = i % len(colors) # Cycle through colors
strip[i] = colors[color_index]
strip.write()
def main():
while True:
display_static_colors()
time.sleep(1) # Pause for 1 second
if __name__ == "__main__":
main()
INPUT¶
Now I tested the INPUT device and I started with touch sensor And i connected it to the LED Light
This is the code I used just make sure that you connect the pin in the right place
// Define the pins for the touch sensor and LED
const int touchSensorPin = 7;
const int ledPin = 6;
void setup() {
// Initialize the serial communication
Serial.begin(9600);
// Set the touch sensor pin as input
pinMode(touchSensorPin, INPUT);
// Set the LED pin as output
pinMode(ledPin, OUTPUT);
// Turn off the LED initially
digitalWrite(ledPin, LOW);
}
void loop() {
// Read the state of the touch sensor
int touchState = digitalRead(touchSensorPin);
// Check if the touch sensor is touched
if (touchState == HIGH) {
// If touched, turn on the LED
digitalWrite(ledPin, HIGH);
Serial.println("Touch sensor is activated: LED ON");
} else {
// If not touched, turn off the LED
digitalWrite(ledPin, LOW);
Serial.println("Touch sensor is not activated: LED OFF");
}
// Wait for a short period before checking again
delay(100);
}
The second Input I tested was the DHT11 sensor that detuct the temprature and humidity
#include <DHT.h>
// Define the pin where the DHT11 data line is connected
#define DHTPIN 6
// Define the type of DHT sensor used
#define DHTTYPE DHT11
// Create an instance of the DHT class
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize the DHT11 sensor
dht.begin();
}
void loop() {
// Wait a few seconds between measurements
delay(2000);
// Read humidity (in %)
float humidity = dht.readHumidity();
// Read temperature in Celsius (default)
float temperature = dht.readTemperature();
// Print the values to the Serial Monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" C");
}
SUMMARY¶
In the end of this week my idea about coding got shifted a lettle bit becouse I didnt like the coding in the beginning it was so mutch of a head ache but now i started to enjoy it little bit where the coding languige become little bit easy for me to understand