Skip to content

13. Input devices

Group assignment:

Probe an input device’s analog levels and digital signals.

Analog input

Analog Input. Signal from the LDR sensor to control the PWM output of Arduino UNO.

Digital input

I designed an interface board for 24Vdc to 5Vdc adapter.


Schematic circuit adapter.


PCB design of circuit adapter.

Digital input. Inductive proximity sensor signal, and signal adapter circuit from 24 Vdc to 5 Vdc

Inductive sensor, 24Vdc to 5Vdc adapter circuit for Arduino UNO

We note that the voltage levels of both analog and digital sensors must be adapted to the operating voltage range of the microcontroller, in some cases this can be done with resistor arrays and in other cases circuits must be designed to adapt the signals.

Individual Assignment:

Measure something: add a sensor to a microcontroller board that you have designed and read it

I worked with HC-SR05 Sensor, it is an ultrasonic sensor. Following we describe this sensor:


Front and back sensor.


Module pin assignments.


Electrical specifications.

First, transmit at least 10us high level pulse to the Trig pin (module automatically sends eight 40K square wave), and then wait to capture the rising edge output by echo port, at the same time, open the timer to start timing. Next, once again capture the falling edge output by echo port, at the same time, read the time of the counter, which is the ultrasonic running time in the air.


Module timing.


Detecting the object.

Calculation equations for distance.


Equations to calculate the distance in centimeters and inches.

Now we apply what we learned from the sensor to measure the distance to an object.

HC-SR04 sensor reading for 15cm and 10cm.

Test 01 Code, for 15cm

const int pinecho = 8;
const int pintrigger = 9;
const int pinled = 13;

unsigned int tiempo, distancia;

void setup() {
  pinMode(pinecho, INPUT);
  pinMode(pintrigger, OUTPUT);
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(pintrigger, LOW);
  delayMicroseconds(2);
  digitalWrite(pintrigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(pintrigger, LOW);

  tiempo = pulseIn(pinecho, HIGH);

  distancia = tiempo / 58;

  if (distancia <= 15) {
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  }
}

Test 02 Code, for 10cm

const int pinecho = 8;
const int pintrigger = 9;
const int pinled = 13;

unsigned int tiempo, distancia;

void setup() {
  pinMode(pinecho, INPUT);
  pinMode(pintrigger, OUTPUT);
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(pintrigger, LOW);
  delayMicroseconds(2);
  digitalWrite(pintrigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(pintrigger, LOW);

  tiempo = pulseIn(pinecho, HIGH);

  distancia = tiempo / 58;

  if (distancia <= 10) {
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  }
}

Download Files