Skip to content

7. Input & Output device

This week we get introduced to I/O devices and how to connect and use them with the microcontroller

Individual assignment

Input device

For the individual assignment, we took a random sensor from a box and the aim was to identify it and know how to connect it and program it as an Input device.

This is the sensor I got and after I search about it I found that this sensor is called a tilt switch sensor. This sensor from its name can detect if it is tilted by having a mercury drop in it.

After that, I found the datasheet for this sensor so I can know how to connect and program it.

DataSheet!

Then I get the parts I needed and I start connecting them.

After making the connections I opened the Arduino IDE and start writing the code.

 int led_pin = 13; // Define the LED interface
int switch_pin = 6; // Definition of mercury tilt switch sensor interface
int val; // Defines a numeric variable
void setup()
{
  pinMode(led_pin, OUTPUT);
  pinMode(switch_pin, INPUT);
}
void loop()
{
  val = digitalRead(switch_pin); // check mercury switch state
  if(val == HIGH)
  {
    digitalWrite(led_pin, HIGH);
  }
  else
  {
    digitalWrite(led_pin, LOW);
  }
}

This code is for testing the sensor if the sensor tilts the built-in led at the microcontroller will work.

Hero-video

Output device

An output device is any piece of hardware equipment which converts information into human-readable form. My output device was a servo motor wich is a motor that sweep 180 degree

I searched the internet for the datasheet of this device click here to access it I used the data sheet to know all of the information I needed to program this device. After that, I start writing the code.

output 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 <= 180; 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(15);                       // waits 15 ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
}

What this code does is will make the servo motor rotating infinitely

Hero-video

Input / Output

After succsfully prgraming the input device and output device, now I tried to cobine them such that if I got a signal from the input device which is the tilt sensor, the output which is the servo motor will rotate. I started by connecting the two device to the microcontroller. Then, I combined the codes wrote above.

IO code

 #include <Servo.h>

int switch_pin = 6; // Definition of mercury tilt switch sensor interface
int val; // Defines a numeric variable
Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position


void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(switch_pin, INPUT);
}
void loop()
{
  val = digitalRead(switch_pin); // check mercury switch state
  if(val == HIGH)
  {
for (pos = 0; pos <= 180; 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(15);                       // waits 15 ms for the servo to reach the position  }
  else
  {
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position  }
}

This code first will read the signal from the input device such that if tilted the output device which is the motor will rotate

Hero-video

I was a fun week for me since I love electronics and I did not face any problem


Last update: August 22, 2022