Skip to content

13. Input Devices

The group work is shown here.

The code is shown below.

HCSR04 Sensor

To detect if a football is on the conveyor belt for my Football Catch & Throw Project, I plan to use a HC-SR04 Ultrasonic sensor:

Picture

The sensor can be hooked up to the Arduino as shown:

Picture

Before using my ATTiny1614 breakout board, I wanted to hook the sensor up to the arduino so that I could easily read serial outputs (I learned from my brother that you can read serial outputs using the ATTiny chip as the processor if the ATMEGA 328 chip is removed from the Arduino - or whichever programmer is being used to provide power - but it is quite a hassle). I decided to follow this tutorial and below shows the football’s distance being read by the Ultrasonic sensor and outputted on the Serial Monitor:

Now that I know the Ultrasonic sensor is functioning, time to take off my training wheels and use a different processor. As shown in output devices week I am a breakout board for my ATTiny1614 before I finalize a custom board for my final project:

Picture

Picture

The conveyor belt’s board design (see Football Catch & Throw Project for further detail on the “conveyor belt”) is shown below:

Picture

I wired the sensor and an LED as shown above and below shows an LED lighting up when an object is detected within 8cm of it. This LED can take the place of the serial monitor for now by giving me some sort of feedback:

For my final project, once a football is detected on the conveyor belt, a servo will spin the ball until the laces are found. So, I wired in a servo based on the diagram above and below shows the result!

The code is shown below:

/* Example code for HC-SR04 ultrasonic distance sensor with Arduino. No library required. More info: https://www.makerguides.com */
#include <Servo.h>
// Define Trig and Echo pin:
#define trigPin 1
#define echoPin 3

#define MAX_PWM 2150
#define MID_PWM 1500
#define MIN_PWM 850

int pos = 0;
Servo myservo;

// Define variables:
long duration;
int distance;

void setup() {
  // Define inputs and outputs:
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  myservo.attach(0); //attaches the servo on pin 0 
  //inMode(pin, OUTPUT);

  //Begin Serial communication at a baudrate of 9600:
  Serial.begin(9600);
  Serial.print("Distance = ");
}

void loop() {
  // Clear the trigPin by setting it LOW:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);

  // Trigger the sensor by setting the trigPin high for 10 microseconds:
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
  duration = pulseIn(echoPin, HIGH);
  // Calculate the distance:
  distance = duration * 0.034 / 2;
  if (distance <= 8) {
    myservo.write(1600);
  } else {
      //digitalWrite(pin, LOW);
    myservo.write(1500);
  }

  // Print the distance on the Serial Monitor (Ctrl+Shift+M):
  Serial.print("Distance = ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(50);

}

All lines without myservo or a PWM variable is code for the HC-SR04 sensor.


Last update: April 26, 2022