5. Input & Output device¶
What is the difference between an input and output device?:¶
Input devices such as keyboards, mice, scanners, and microphones are used to provide data and instructions to the computer, converting physical input into digital signals that the computer can process, while output devices like monitors, printers, and speakers are used to present the processed information from the computer in a human-readable or machine-readable form, converting the digital signals back into a physical output that the user can interact with, representing the key functional difference between these two types of computer peripherals.
Output devices:¶
Traffic Light¶
Code¶
from machine import Pin
import time
# Set up GPIO pins
red_pin = Pin(12, Pin.OUT)
yellow_pin = Pin(13, Pin.OUT)
green_pin = Pin(14, Pin.OUT)
# Traffic light sequence
while True:
# Red light
red_pin.value(1)
yellow_pin.value(0)
green_pin.value(0)
time.sleep(5)
# Yellow light
red_pin.value(0)
yellow_pin.value(1)
green_pin.value(0)
time.sleep(2)
# Green light
red_pin.value(0)
yellow_pin.value(0)
green_pin.value(1)
time.sleep(10)
# Green light
red_pin.value(0)
yellow_pin.value(0)
green_pin.value(0)
time.sleep(0.6)
# Green light
red_pin.value(0)
yellow_pin.value(0)
green_pin.value(1)
time.sleep(0.6)
# Green light
red_pin.value(0)
yellow_pin.value(0)
green_pin.value(0)
time.sleep(0.6)
# Green light
red_pin.value(0)
yellow_pin.value(0)
green_pin.value(1)
time.sleep(0.6)
# Green light
red_pin.value(0)
yellow_pin.value(0)
green_pin.value(0)
time.sleep(0.6)
# Green light
red_pin.value(0)
yellow_pin.value(0)
green_pin.value(1)
time.sleep(0.6)
# Green light
red_pin.value(0)
yellow_pin.value(0)
green_pin.value(0)
time.sleep(0.6)
# Green light
red_pin.value(0)
yellow_pin.value(0)
green_pin.value(1)
time.sleep(0.6)
# Green light
red_pin.value(0)
yellow_pin.value(0)
green_pin.value(0)
time.sleep(0.6)
# Yellow light
red_pin.value(0)
yellow_pin.value(1)
green_pin.value(0)
time.sleep(1.5)
# Red light
red_pin.value(1)
yellow_pin.value(0)
green_pin.value(0)
time.sleep()
RGB Led¶
Connection¶
Code¶
from machine import Pin
import time
RGBLED = Pin(17, Pin.OUT)
RGBLED2 = Pin(18, Pin.OUT)
RGBLED.value(1)
RGBLED2.value(1)
time.sleep(2)
RGBLED.value(0)
RGBLED2.value(0)
In-put devices:¶
Pressure Sensor¶
Connection¶
Code¶
#include <SPI.h>
#include <Adafruit_DPS310.h>
Adafruit_DPS310 dps;
Adafruit_Sensor *dps_temp = dps.getTemperatureSensor();
Adafruit_Sensor *dps_pressure = dps.getPressureSensor();
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
Serial.println("DPS310");
if (! dps.begin_I2C()) {
Serial.println("Failed to find DPS");
while (1) yield();
}
Serial.println("DPS OK!");
// Setup highest precision
dps.configurePressure(DPS310_64HZ, DPS310_64SAMPLES);
dps.configureTemperature(DPS310_64HZ, DPS310_64SAMPLES);
dps_temp->printSensorDetails();
dps_pressure->printSensorDetails();
}
void loop() {
sensors_event_t temp_event, pressure_event;
if (dps.temperatureAvailable()) {
dps_temp->getEvent(&temp_event);
Serial.print(F("Temperature = "));
Serial.print(temp_event.temperature);
Serial.println(" *C");
Serial.println();
delay (1000);
}
// Reading pressure also reads temp so don't check pressure
// before temp!
if (dps.pressureAvailable()) {
dps_pressure->getEvent(&pressure_event);
Serial.print(F("Pressure = "));
Serial.print(pressure_event.pressure);
Serial.println(" hPa");
Serial.println();
delay (1000);
}
}
Capacitive touch¶
Connection¶
Code¶
#include <Wire.h>
#include "Adafruit_MPR121.h"
#ifndef _BV
#define _BV(bit) (1 << (bit))
#endif
// You can have up to 4 on one i2c bus but one is enough for testing!
Adafruit_MPR121 cap = Adafruit_MPR121();
// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
void setup() {
Serial.begin(9600);
while (!Serial) { // needed to keep leonardo/micro from starting too fast!
delay(10);
}
Serial.println("Adafruit MPR121 Capacitive Touch sensor test");
// Default address is 0x5A, if tied to 3.3V its 0x5B
// If tied to SDA its 0x5C and if SCL then 0x5D
if (!cap.begin(0x5A)) {
Serial.println("MPR121 not found, check wiring?");
while (1);
}
Serial.println("MPR121 found!");
}
void loop() {
// Get the currently touched pads
currtouched = cap.touched();
for (uint8_t i=0; i<12; i++) {
// it if is touched and wasnt touched before, alert!
if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
Serial.print(i); Serial.println(" touched");
}
// if it was touched and now isnt, alert!
if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
Serial.print(i); Serial.println(" released");
}
}
// reset our state
lasttouched = currtouched;
// comment out this line for detailed data from the sensor!
return;
// debugging info, what
Serial.print("\t\t\t\t\t\t\t\t\t\t\t\t\t 0x"); Serial.println(cap.touched(), HEX);
Serial.print("Filt: ");
for (uint8_t i=0; i<12; i++) {
Serial.print(cap.filteredData(i)); Serial.print("\t");
}
Serial.println();
Serial.print("Base: ");
for (uint8_t i=0; i<12; i++) {
Serial.print(cap.baselineData(i)); Serial.print("\t");
}
Serial.println();
// put a delay so it isn't overwhelming
delay(1000);
}
Last update:
August 16, 2024