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 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


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


  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() {


  while (!Serial);

  if (!HTS.begin()) {

    Serial.println("Failed to

     initialize humidity

      temperature sensor!");

    while (1);

void loop() {

  // read all the sensor values

  float temperature =


  float humidity    =


  // print each of the sensor values

  Serial.print("Temperature = ");


  Serial.println(" °C");

  Serial.print("Humidity    = ");


  Serial.println(" %");

  // print an empty line


  // wait 1 second to print again




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 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.


  1. Ground to ground

  2. Power to Vin

  3. Digital to D6


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


 #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


         // tell servo to go to

          position in variable



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

  for (pos = 180; pos >= 0; pos

    -= 1) { // goes from 180 degrees to 0 degrees

              // tell servo to go to position in variable 'pos'


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


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


 #include <Servo.h>

Servo servo1;

int trigPin = 9;

int echoPin = 8;

long distance;

long duration;

void setup()



 pinMode(trigPin, OUTPUT);

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


void loop()




  if(distance <=9)





void ultra_sonic()


  digitalWrite(trigPin, LOW);


  digitalWrite(trigPin, HIGH);


  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin,


  distance = duration*0.034/2;



Last update: January 23, 2022