7. Input & Output device¶
This week, we learned how to program input and output devices so that they work together to produce the desired end results.
Research¶
What is the difference between an input and output device?¶
An input device sends information to a computer system for processing, and an output device reproduces or displays the results of that processing. Input devices only allow for input of data to a computer and output devices only receive the output of data from another device.
Most devices are only input devices or output devices, as they can only accept data input from a user or output data generated by a computer. However, some devices can accept input and display output, and they are called I/O devices (input/output devices).
The table bellow shows the key differences between input devices and output devices:
Sources
Individual assignment¶
Within this week I experimented with 4 different inputs and outputs, which are the servo motor, RGB LED, ultrasonic sensor, and the big sound sensor.
Output Devices¶
Output 1: Servo Motor¶
A servo motor is a type of motor that allows us to regulate the position, acceleration, and velocity of an object while it rotates.
Connection
After learning about the device, I connected the wires according to the diagram below.
Then I borrowed the following code from the internet and modified it to fit my needs. The results are shown in the video after the code.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 120; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos = 120; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
}
Output 2: RGB LED¶
RGB LED output refers to the colors produced by a light-emitting diode (LED) capable of outputting red, green, and blue light. An RGB LED can produce a wide spectrum of colors by regulating the intensity of each hue.
The output of an RGB LED is often set in terms of the intensity of each color component, which is typically stated as a value between 0 and 255 for each color. This is known as the RGB color model, and it is used to represent colors on digital displays and other electrical devices.
Connection
After learning about the device, I connected the wires according to the diagram below.
Then I borrowed the following code from the internet and modified it to fit my needs. The results are shown in the video after the code.
// Simple RGB LED Loop Code for BLE Nano
int rgb_led_pins[] = {2,3,4}; // pins used for RGB LED wiring
void setup() {
for (int ii=0; ii<sizeof(rgb_led_pins)/sizeof(int);ii++){
pinMode(rgb_led_pins[ii],OUTPUT); // set the RGB LED pins as outputs
}
}
void loop() {
// loop through the RGB LED pins and turn them on/off
for (int ii=0; ii<sizeof(rgb_led_pins)/sizeof(int);ii++){
digitalWrite(rgb_led_pins[ii], HIGH); // led high
delay(200); // wait 100ms
digitalWrite(rgb_led_pins[ii], LOW); // led low
delay(200); // wait 100ms
}
}
Input Devices¶
Input 1: Ultrasonic Sensor¶
What is an Ultrasonic Sensor?
An ultrasonic sensor is a device that uses ultrasonic sound waves to determine the distance between two objects.
An ultrasonic sensor employs a transducer to send and receive ultrasonic pulses that relay information about the proximity of an object.
How Ultrasonic Sensors Work?
The working principle of this device is simple. It sends an ultrasonic pulse out, which travels through the air, and if there is an obstacle or object, it will bounce back to the sensor. By calculating the travel time and the speed of sound, the distance can be calculated.
Connection
After learning about the device, I connected the wires according to the diagram below.
Then I borrowed the following code from the internet and modified it to fit my needs. The results are shown in the video after the code.
// defines pins numbers
const int trigPin = 2;
const int echoPin = 4;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}
Input 2: Big Sound Sensor¶
A sound sensor is a simple, easy-to-use device that is used to detect sound waves traveling through the air. It can also measure sound’s intensity and convert it to an electrical signal which we can read through a microcontroller.
Connection
After learning about the device, I connected the wires according to the diagram below.
Then I borrowed the following code from the internet and modified it to fit my needs. The results are shown in the video after the code.
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(500);
}
Combining Input and Output Devices¶
Finally, I combined input devices and output devices to perform desired functions.
Big sound RGB arduino¶
Connection
Code
int led = 6;
int sound_digital = 7;
int sound_analog = A0;
void setup(){
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(sound_digital, INPUT);
}
void loop(){
int val_digital = digitalRead(sound_digital);
int val_analog = analogRead(sound_analog);
Serial.print(val_analog);
Serial.print("\t");
Serial.println(val_digital);
if (val_digital == HIGH)
{
digitalWrite (led, LOW);
delay(1000);
}
else
{
digitalWrite (led, HIGH);
}
}
End result
Push Button RGB LED Color Change¶
Connection
Code
#define button 2
#define redLED 4
#define greenLED 3
#define blueLED 5
int state = 0;
int old = 0;
int buttonPoll = 0;
void setup() {
pinMode(button,INPUT);
//RGB LED set as output
pinMode(redLED,OUTPUT);
pinMode(greenLED,OUTPUT);
pinMode(blueLED,OUTPUT);
digitalWrite(redLED,LOW);
digitalWrite(greenLED,LOW);
digitalWrite(blueLED,LOW);
}
void loop() {
buttonPoll = digitalRead(button);
if(buttonPoll == 1) {
delay(50);
buttonPoll = digitalRead(button);
if(buttonPoll == 0) {
state = old + 1;
}
}
else {
delay(100);
}
switch (state) {
case 1: //Red color
digitalWrite(redLED,LOW);
digitalWrite(greenLED,HIGH);
digitalWrite(blueLED,HIGH);
old = state;
break;
case 2: //Green color
digitalWrite(redLED,HIGH);
digitalWrite(greenLED,LOW);
digitalWrite(blueLED,HIGH);
old = state;
break;
case 3: //Blue color
digitalWrite(redLED,HIGH);
digitalWrite(greenLED,HIGH);
digitalWrite(blueLED,LOW);
old = state;
break;
case 4: //Purple color
digitalWrite(redLED,LOW);
digitalWrite(greenLED,HIGH);
digitalWrite(blueLED,LOW);
old = state;
break;
case 5: //White color
digitalWrite(redLED,HIGH);
digitalWrite(greenLED,HIGH);
digitalWrite(blueLED,HIGH);
old = state;
break;
default:
digitalWrite(redLED,LOW);
digitalWrite(greenLED,LOW);
digitalWrite(blueLED,LOW);
old = 0;
break;
}
}
End result
Input/Output Interfacing Insights¶
During this week I learned how to interface input devices (Ultrasonic and Big Sound Sensors) with a microcontroller to measure physical properties accurately. For output devices (Servo Motor and RGB LED), I learned how to control the physical property being manipulated by the microcontroller. By combining input and output devices, I was able to create unique applications, such as the Big Sound RGB Arduino and Push Button RGB LED Color Change. Overall, interfacing input and output devices with a microcontroller requires attention to physical properties and understanding of how the devices work.