Skip to content

6. Input & Output Devices

It is required to use an input component and see its output in the serial monitor of Arduino mkr1010

I used touch sensor it is a type of device that captures and records physical touch or embrace on a device and/or object. It enables a device or object to detect touch or near proximity, typically by a human user or operator and it requires 3.3 to 5 volts. It is a digital sensor.

This is the touch sensor:

-This is the code –> In this code when the sensor is touched it will shown in the serial monitor that the sensor is touched.

// constants won't change. They're used here to set pin numbers:
const int SENSOR_PIN = A1; // the Arduino's input pin that connects to the sensor's SIGNAL pin 

// Variables will change:
int lastState = LOW;      // the previous state from the input pin
int currentState;         // the current reading from the input pin

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // initialize the Arduino's pin as an input
  pinMode(SENSOR_PIN, INPUT);
}

void loop() {
  // read the state of the the input pin:
  currentState = digitalRead(SENSOR_PIN);

  if(lastState == LOW && currentState == HIGH)
    Serial.println("The sensor is touched");

  // save the the last state
  lastState = currentState;
}

Here is the output in the serial monitor

Output

I used MKR1010 with servo motors to see the output

-I used servo motors as an output device, is a self-contained electrical device, that rotate parts of a machine with high efficiency and with great precision. The output shaft of this motor can be moved to a particular angle, position and velocity that a regular motor does not have. It requires 4 to 7.2 volts. It turn the signal it receives to movement.

This is the servo motor:

low speed code

for this code, the delay will control the speed of the servo motor, also the position of the motor can be edited from 0 to 180

#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(2);  // attaches the servo on pin 9 to the servo object
}

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

High speed code

for this code, the delay is decreased to control the speed of the servo motor, also the position of the motor can be edited from 0 to 180

#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(2);  // attaches the servo on pin 9 to the servo object
}

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

Connection between servo and touch sensor

It is required to connect both the input and output devices together and see how it will works:

The circuit diagram

The servo code

-In this code, it will rotate the servo motor when the user touch the sensor.

#include <Servo.h>

#define SERVO_PIN 3
#define TOUCH_PIN A0

Servo servo;

void setup() {
  pinMode(TOUCH_PIN, INPUT);

  servo.attach(SERVO_PIN);
  servo.write(90);  // Set the initial position of the servo to 90 degrees
}

void loop() {
  int touchValue = analogRead(TOUCH_PIN);

  if (touchValue < 500) {  // Adjust this threshold value according to your touch sensor
    servo.write(180);  // Rotate the servo to 180 degrees
    delay(1000);  // Wait for 1 second
    servo.write(90);  // Rotate the servo back to 90 degrees
    delay(50);  // Wait for another 1 second
  }
}

Last update: September 7, 2023