Skip to content

8. Input/Output devices

This Weeks’ Task

Individual assignment 1- Measure something: add a sensor to a microcontroller board and read it.

2- Add an output device to a microcontroller board and program it to do something.

3- Document your work

===============================================================

OUTput devices

First, lets talk about output devices. An output device takes the electrical energy that we provide it for it and turns it into a deferent type of physical outputs like lights, sounds, heat and more.

====================

OUTput devices (Buzzer)

The buzzer takes the electricity and turns it to sound.

If we want to produce different sounds and melody, we need to generate the plate inside the buzzer to make and vibrate it to create the sounds.

So if we give it different signals, it will vibrate differently to create different sounds.

So we have something called the tone function that will generate certain frequencies to certain durations by giving it to a certain pin

tone(pin, frequency, duration)

This is the link of the tone function

To try buzzer, we used the Example in the Arduino IDE. You can find it by going to FILES, EXAMPLES, DIGITALS, toneMELODY.

And this is how it turned out.

====================

Harry potter melody

I’ve searched for different song as I was interested to see how the buzzer actually works. I found Harry potter’s song from

this link

I tried it and here how it came out!

===============================================================

INput devices

It is a device that gives data, as it takes data from the outer world, capture it and transfer it tp the microcontroller.

====================

Analog sensor

It determine colors and mainly white and black. As it gives different values if it is projected to white or black colors by the two bulbs in the sensor.

We should use the analog pins which is the (A0,A1,…) in the microcontroller as it does not understand the digital pins (D1, D2, D3,…).

====================

Button

It can only input if we press it or un pressed it by high or low values.

===============================================================

Combining input and output devices (Servo motor and Button)

So to try things out, I decided firstly to combine an input device which is the button with an output device which is the Servo Motor.

This is the video that helped me understand more

1- First I defined the servo pin =2

2- defined the push button pin= 3

3- Defining the initial angle of the motor, for me = 90 *but if you want to change it, it is recommended to set it 1 degree lower (between 1 and 179)

3- initial angle step = the value will increment from initial angle by 10.

4- Defining the minimum and maximum angles of the motors.

5- You can coose 1 or 2

A- Const int type = 1 (it will began from 0 to 180 and then comes back to 0 as an angle)

By using type 1 this is the result

So in this outcome, you can see that the motor will move to go to the angle 0, all the way to 180 and then it go’s back to 0 again. and as you can see in the values in the image below, the initial angle step is increasing by 10 and decreasing by 10 as I specified it earlier.

B- Const int type = 2 (it will starts from 180 to 0 as an angle)

By using type 2 this is the result

In this outcome, the motor started from 180 moving all the way to the 0 angle. This is demonstrated within the serial monitor in the image below.

#include <Servo.h>

Servo myservo;
#define servoPin 2
#define pushButtonPin 3

int angle =90;    // initial angle  for servo (beteen 1 and 179)
int angleStep =10;
const int minAngle = 0;
const int maxAngle = 180;

const int type =2;

int buttonPushed =0;

void setup() {
  Serial.begin(9600);
  myservo.attach(servoPin);
  pinMode(pushButtonPin,INPUT_PULLUP);
   Serial.println(" Servo Button ");
   myservo.write(angle);//initial position
}

void loop() {
  if(digitalRead(pushButtonPin) == LOW){
    buttonPushed = 1;
  }
   if( buttonPushed ){
  // change the angle for next time through the loop:
  angle = angle + angleStep;

    // reverse the direction of the moving at the ends of the angle:
    if (angle >= maxAngle) {
      angleStep = -angleStep;
        if(type ==1)
        {
            buttonPushed =0;                   
        }
    }

    if (angle <= minAngle) {
      angleStep = -angleStep;
       if(type ==2)
        {
            buttonPushed =0;       
        }
    }

    myservo.write(angle); // move the servo to desired angle
      Serial.print("Moved to: ");
      Serial.print(angle);   // print the angle
      Serial.println(" degree");    
  delay(100); // waits for the servo to get there
   }

}
 ```
===============================================================


#### Combining input and output devices (Servo motor and Sensor)

I wanted to test the sensor by using the motor.

1- I defined the sensor pin by A0.

2- Defined the servo motor by D9.

3- Set up the pin mode to sensor pin as an input.

4- Defined the value, if the value is 0 which is white or nothing in front of the sensor, the motor will not move.

5- Defined the value, if the value is 1 which is black or something in front of the sensor, the motor will move to 90 degrees.

include

Servo tap_servo;

int sensor_pin = A0; int tap_servo_pin =9; int val;

void setup () { pinMode (sensor_pin, INPUT); tap_servo.attach(tap_servo_pin);

} void loop (){ val = digitalRead (sensor_pin);

// black if (val == 0) {tap_servo.write(0);

}

if (val == 1) {tap_servo.write(90);

} } ```


Last update: September 11, 2021