HOME ABOUT ASSIGNMENT FINAL PROJECT CONTACT

WEEK 11 - INPUT DEVICES

Assignment
individual assignment : measure something: add a sensor to a microcontroller board that you have designed and read it
group assignment : measure the analog levels and digital signals in an input device
Here is the link to Group assignment.

HC-SR04(ultrasonic sensor)



HC-SR04 HC-SR04
HC-SR04
HC-SR04

TESTING



First of all, I want to experience sonar sensor. I tried to measure distance with Arduino uno. I searched tutorial for sonar sensor and followed. Here is code.


int trig = 8;
int echo = 9;
int led = A0;

void setup() {
Serial.begin(9600);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(led, OUTPUT);
}

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

unsigned long duration = pulseIn(echo, HIGH);
float distance = duration / 29.0 / 2.0;

Serial.print(distance);
Serial.println("cm");
if (distance < 10){
digitalWrite(led, HIGH);
}

else {
digitalWrite(led, LOW);
}
delay(200);
}


testing testing
testing
HC-SR04 has 4pins those are 2pins at the outside are VCC and GND, 2pins inside are TRIG and ECHO that are related to ultrasonic. I put sonar sensor HC-SR04 on bread board and connected Arduino uno with jumper wire. VCC, GND and pin 12, 13. I debugged the code and it was fine. After setting tools, I programmed the Arduino and checked serial monitor. It was quite accurate with the distance. And I also tried changing delay.

I moved sketchbook in front of sensor, it showed distance with unit cm. Data-sheet said minimum distance is 2cm and when I move object closer than 2cm, it shows 0.8 and get rag. And I tried for farthest distance and it was about 2.4m.

testing
testing

MAKING BOARD



Making board

I followed reference in the class page. I used Eagle to design the board. But there wasn’t HC-SR04 in Fab library so I downloaded the library here. (link)
I installed library and add HC-SR04. and I added every components like resistor, capacitor, ATtiny 45, and headers. I labelled every part and connected each other.
HC-SR04
Then, I made board, placed every parts and draw routes.
HC-SR04
And I exported to png and modified though hole to rectangular because I want to solder the sensor. And create outline .png file.

I open those files on fabmodules and it was fine.
fabmodule fabmodule
fabmodule fabmodule
fabmodule
Milling board Milling was fine and I soldered every components.
milling milling
milling milling

PROGRAMMING



First I tried to program ATtiny 45 with Arduino because I tried before. I connected fabISP to board, FTDI board to board too.
HC-SR04
I changed PIN number to ATtiny 45’s PIN number 3,4. And it said ‘Serial was not declared in this scope. I searched to find out what is problem with this error message. But I couldn’t find out. After regional review, I got some clue from Seonghee Kim. To program the board include serial, I need to do some before work to Arduino understand serial code. those are ‘SoftwareSerial” and ‘SendonlySoftwareSerial’. I don’t understand those perfectly but I understand the reason why it is needed. I downloaded those files and moved those to the folder that has sketchbook files in library. Then I added the code
#include
#include
I checked again but the same error message came out. I asked to instructor and realized I missed the code about the pins. So I followed his code. Erase #include line and added ‘SoftwareSerial my Serial (0,2);’.
Then I could program my board. I opened serial monitor window and checked what board says.
This is the codes.
milling
Here’s the video I measure the distance with sonar sensor.

I also tried with python. But it said ‘No module named serial’ I thought it’s about serial again. I should install Pyserial or something.

And I also tried the C code that Neil provided. I programed with my fabISP and opened Arduino serial monitor but it showed special character seems like alien use.

milling
milling
milling

Here are my works.



Download
CONTACT

WHERE I WORK

I'd love your feedback!

Seoul. KOREA
Phone: +082 1041950935
Email: jshjshjsh93@gmail.com

Feel free to contact! , or leave me a note: