Eytan Mann


Week 8 - Input Devices


Working with Hc-sr04 Ultrasonic Distance Sensor
Ultrasonic is a high frequency sound (typically 40KHz is used). A short burst of wave ( often 8 cycles) is sent out by the speaker. The microphone listens for an echo. The time taken is recorded. Using calculation L = C X T/2
L : Length
C: Speed of sound in air (344m/s at 20 degrees celcius)
T : time taken is divided by 2
The distance is determined.

Project 02a Product features:
Ultrasonic ranging module HC - SR04 provides 2cm - 400cm non-contact measurement function, the ranging accuracy can reach to 3mm. The modules includes ultrasonic transmitters, receiver and control circuit. The basic principle of work:
(1) Using IO trigger for at least 10us high level signal
(2) The Module automatically sends eight 40 kHz and detect whether there is a pulse signal back.
(3) IF the signal back, through high level , time of high output IO duration is the time from sending ultrasonic to returning.
Test distance = (high level time×velocity of sound (340M/S) / 2

Wire connecting direct as following:
 5V Supply 
Trigger Pulse Input
 Echo Pulse Output
 0V Ground

Download NewPing Library
Put the "NewPing" folder in "libraries\".
In the Arduino IDE, create a new sketch (or open one) and select from the menubar "Sktech->Import Library->NewPing".
Download the SerialMonitor lib for serial monitorhttps://www.arduino.cc/en/Reference/Serial

Program code - Arduino
#define TRIGGER_PIN 12;
#define ECHO_PIN 11;
#define MAX_DISTANCE 200;


void setup() {

void loop() {
unsigned int uS = sonar.ping();
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM);

1. Load the sketch
2. Plug in FTDI and check Serial Monitor in Arduino IDE
Project 02a
Project 02a
To connect the Sonar I bent the pins so they won't connect to the routing behind it.

Project 02a
After programming the Attiny45 succesully, for some reason the arduino code did not work, the serial monitor did not recieve input, only a strange character appeared..
Went on to to try the process using the classes C code and Make file.

Installing AVRDude and running the Python to visualize
Project 02a
To visualize the input an additional Python code (from the class site) was used. To execute the Python code, used the shell

python hello.HC-SR04.py /dev/tty.usbserial-AL01TQBL

This worked partially:/

The distance meter appeared showing the distance, but is kept-on getting stuck on the same distance: 141 cm.
No diagnosis just a few possible problems, such as: The sensor is busted / the soldering the bad... who realy known.

Decided to make an 'Hello Arduino' board, thinking its a good investment to try different sensors withour re-milling and re-soldering at every problem

Designed a board based on the classes Hello Arduino Board (embedded programming week page) Project 02c
This new board is using the ATMega Project 02c

The architecture of this microcontroller is alot more condensed. It has many “free” pins to be used for connecting sensors and motors, for my project. The only problem with this is that soldering is going to be exceedingly difficult because of the super-fine routes.
Milling was also difficult. To mill with the modela I had to experiment with the offset and tool width.
Changes the definitions to 1 offset of the 1/64 mill, and 0.03 tool width, until the paths seemed to be OK in the preview image..
Project 02c
Project 02c
Project 02c
Project 02c