Skip to content

13. Input devices

Group assignment:

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

Individual Assignment:

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

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);
  }
}