Project Background

During week 4 we were asked to program an Arduino. I wanted to start building towards my final project, which will likely include some motion detection. With that, I sought to test out an ultrasonic sensor input with two outputs.

Process



Materials

  • Arduino UNO REV3
  • Ultrasonic distance sensor HC-SR04
  • DC motor
  • LED
  • 10k ohm resistor


Goals

When an object is in front of the sensor, it should accurately report the distance on the Serial Monitor, turn the LED on, and start the motor. If the object is removed, the motor and LED should turn off.

Using the Arduino

I built to the specifications and adapted this tutorial. I learned that the serial monitor should be set to Serial.begin(9600) in order to print back to the Serial Monitor. I also learned that this formula would give me the distance duration*0.034/2.

The motor would not turn on despite significant debugging. So I tried the set up in TinkerCAD and it ran with no problem. Here is the diagram and code.

arduino set up with only led working, not motor

code for arduino

Time to debug. I tested to make sure the motor was working using a separate power supply. I then replaced the motor with a multimeter. It showed 5V, the amount needed to power the motor.

multimeter in place of the motor

Finally, I found the issue. It turns out that digital pins did not have enough current to drive the motor so I needed a transistor. Here is the new diagram:

diagram of an arduino set up with the transistor added

gif of an arduino set up with the transistor added

It worked! A good reminder that voltage and current are not the same thing. Time to report this bug to TinkerCAD...