Skip to content

7. Input & Output device

This week we worked on input and output devices, connected to Arduino Nano 33 Ble microcontroller.

General information

An input device is something you connect to a computer that sends information into the computer. An output device is something you connect to a computer that has information sent to it.

Input

Input devices are used to allow us to enter information into a computer system. This might be, for example, to control a character in a game, click on a shortcut icon on your desktop, or type data into a spreadsheet. To test this, we tried to use the microcontroller to sense temperature and connected it to the computer to read the results.

Reading temperature

Steps

Opened a new page in Arduino

Then I had to copy the HTS221 library link from Arduino website to download it

Opened Sketch > Include library > Manage libraries

Pasted the link and installed the HTS221 library which is made to read temperature and humidity sensors of Nano 33 BLE sense.

Now we can find the read sensors code with the examples

Code

 /*
  HTS221 - Read Sensors

  This example reads data from the on-board HTS221 sensor of the
  Nano 33 BLE Sense and prints the temperature and humidity sensor
  values to the Serial Monitor once a second.

  The circuit:


  -Arduino Nano 33 BLE Sense

  This example code is in the public domain.

*/

 #include <Arduino_HTS221.h>

void setup() {

  Serial.begin(9600);

  while (!Serial);


  if (!HTS.begin()) {

    Serial.println("Failed to

     initialize humidity

      temperature sensor!");

    while (1);
  }
}

void loop() {

  // read all the sensor values

  float temperature =

   HTS.readTemperature();

  float humidity    =

   HTS.readHumidity();


  // print each of the sensor values

  Serial.print("Temperature = ");

  Serial.print(temperature);

  Serial.println(" °C");

  Serial.print("Humidity    = ");

  Serial.print(humidity);

  Serial.println(" %");

  // print an empty line

  Serial.println();

  // wait 1 second to print again

  delay(1000);

}

Results

To read the results, I uploaded the code and opened the serial monitor from tools section.

The serial monitor window appeared showing the temperature of the room

To test the change of temperature, I decided to increase it by holding the microcontroller

Now we can notice the increasement of temperature appearing in the window

Output

Output devices provide data in myriad different forms, some of which include audio, visual, and hard copy media. The devices are usually used for display, projection, or for physical reproduction. Monitors and printers are two of the most commonly-known output devices used with a computer. To test this device I chose the servo.

Servo motor

Servo motors or “servos”, as they are known, are electronic devices and rotary or linear actuators that rotate and push parts of a machine with precision. Servos are mainly used on angular or linear position and for specific velocity, and acceleration. This time we needed to connect the servo motor with the micro controller by jumper wires.

CONNECTIONS:

  1. Ground to ground

  2. Power to Vin

  3. Digital to D6

Steps

Went to Examples > Servo > Sweep

The servo code appeared

Changed pos to the value of 180 which is the maximum movement degree for this servo and changed the delay to 1000

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(6);  // attaches

   the servo on pin 6 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(1000);         

                // 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(0);         

                // waits 15 ms for the servo to reach the position
  }
}

Results

Connected the microcontroller to computer and uploaded the code, and now we can see the servo moving to 180 degrees

Input and Output

Ultrasonic sensor and Servo

For last task, we were asked to merge between the two devices (input & output). I chose Ultrasonic sensor and servo because they are the two I need for my final project.

I found this useful video on YouTube and followed its steps

Needs and connections

  1. Microcontroller
  2. Ultrasonic sensor
  3. Servo motor
  4. Jumper wires

And this diagram below shows how everything is connected

Code

 #include <Servo.h>

Servo servo1;

int trigPin = 9;

int echoPin = 8;

long distance;

long duration;

void setup()

{

servo1.attach(7);

 pinMode(trigPin, OUTPUT);

 pinMode(echoPin, INPUT);// put
 your setup code here, to run once:

}

void loop()

{

  ultra_sonic();

  servo1.write(90);

  if(distance <=9)

  {

  servo1.write(270);

  }

}

void ultra_sonic()

{

  digitalWrite(trigPin, LOW);

  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin,

     HIGH);

  distance = duration*0.034/2;

  }

Results


Last update: January 23, 2022