Introduction
This is my first project here, so be patient with me. I'm also sorry for my English, as you probably already noticed, I'm not a native speaker :D But I hope, you will find something helpfully here. If anything is not 100% clear, or need help, just ask. I will do my best, but it's impossible to make no mistakes, so if you find one, let me know.The purpose of the project
So arduino. This project is basically for the beginners about how to control LED-s and use an ultrasonic distance meter sensor without an additional library. If there is nothing before the sensor, shows 0 led. When something is coming closer and closer at 30 cm the first led shows up, at 20 cm the second and at 10 cm the third. As you can see here:What you need:
3 LED-s3 220 ohm resistors (recommended, but not absolutely necessary)
1 HC-SR04 ultrasonic distance sensor
an Ardunio microcontroller and a breadboard with wires
First step: Wiring
The wiring is very simple. First you need 3 LEDs and 3 resistors. (I've used 220 ohm resistors, but you have some with other parameter, or you don't have resistance at all, it's not a so big problem. Theoretically you always have to use resistance before LED, because otherwise too many current is going to flow through the LED and it will kill it. But I tried this without any resistance, and it worked fine and none of my LEDs died.) So connect the resistors to the arduino ground (GND) pin, then connect each resistance to a LED lastly connect the LED-s to 2, 4, 6 pin. Now the LED-s are ready, you need to wire the ultrasonic sensor. I've used a HC-SR04 module, because it's cheap and more than enough for this function. Here it is:As you can see, it has 4 pin. Connect the Vcc to 5V and GND to the ground pin. Connect the trig pin to 12 and echo pin to 13. (Of course you can change these numbers in the code)
And here is the breadboard:
Second step: programming the microcontroller
If you are ready with wiring, you have only one job left: just copy and upload the code to your ardunio. The code also available here: http://codepad.org/TvZQ2v7O//3 leds on pin 2, 4, 6 int a = 2; int b = 4; int c = 6; //the ultrasonic sensors trig and echo are connected to pin 12 and 13 #define trigPin 12 #define echoPin 13 //run only once at the beginning. Define pinMode-s depending on we gonna send or recive data from that void setup() { //LEDs pinMode(a, OUTPUT); pinMode(b, OUTPUT); pinMode(c, OUTPUT); //distance sensor pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } // the loop routine runs over and over again forever: void loop() { int duration, distance; //send the sound digitalWrite(trigPin, HIGH); delayMicroseconds(1000); //wait a little bit digitalWrite(trigPin, LOW); //turn off the sound //perceive when it come back duration = pulseIn(echoPin, HIGH); //convert from time to distance (in cm) distance = (duration/2) / 29.1; //first turn off all the leds digitalWrite(a, LOW); digitalWrite(b, LOW); digitalWrite(c, LOW); if(distance < 10) //if very close: 3 leds on { digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); } else if(distance < 20) //if close: 2 leds on { digitalWrite(a, HIGH); digitalWrite(b, HIGH); } else if(distance < 30) //if not so far: only 1 led on { digitalWrite(a, HIGH); } }
Summary
It was one of my first project, and I recommend it for absolute beginners too. If you get stuck, just let me know, and I'm going to try to help you. More complicated projects are coming soon :PBest regards:
Mark
great tutorial, thank you! i've done it, and it's worked properly:)
ReplyDelete