Shop OBEX P1 Docs P2 Docs Learn Events
Using the Ping))) sensor with an ATmega 328p microcontroller — Parallax Forums

Using the Ping))) sensor with an ATmega 328p microcontroller

mirzammirzam Posts: 2
edited 2014-01-06 12:19 in Accessories
Hallo I am beginner at embedded c programming. I have ATmega 328p microcontrol that is programmed in C, want to use Ping sensor and I want to blink LED when the objects is 20 cm from the sensor.

Can anyone help me with the code or how to star. I understand how Ping sensor works but i can not write code.

please help

Comments

  • FranklinFranklin Posts: 4,747
    edited 2013-11-29 13:21
    There is code to read the ping with Arduino here http://arduino.cc/en/Tutorial/ping It should be a start to writing 328 code if you are not using an arduino. To blink the led you need to test for the distance and when it reaches 20cm call some code to blink the LED.
  • mirzammirzam Posts: 2
    edited 2013-11-30 08:41
    problem is that I don't know how to time a puls..
  • Mike GreenMike Green Posts: 23,101
    edited 2013-11-30 13:45
    We can't really help you with that level of detail on programming a non-Parallax product. There must be some function calls in one of the libraries available to you for your embedded C compiler that will do the sort of timing you need. The Arduino library has a pulse-in function that does this. You might get some ideas from that.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2013-12-03 15:18
    I updated the subject of your thread to indicate the help you actually need. The original subject gave the impression you may have had a malfunctioning PING))) Sensor.
  • jtilghmanjtilghman Posts: 67
    edited 2014-01-06 12:19
    Franklin wrote: »
    There is code to read the ping with Arduino here http://arduino.cc/en/Tutorial/ping It should be a start to writing 328 code if you are not using an arduino. To blink the led you need to test for the distance and when it reaches 20cm call some code to blink the LED.
    const int pingPin = 11;
     unsigned int duration, inches;
      void setup() {
       Serial.begin(9600);
     }
     void loop() {
       pinMode(pingPin, OUTPUT);          // Set pin to OUTPUT
      digitalWrite(pingPin, LOW);           // Ensure pin is low
       delayMicroseconds(2);
       digitalWrite(pingPin, HIGH);       // Start ranging
       delayMicroseconds(5);              //   with 5 microsecond burst
       digitalWrite(pingPin, LOW);        // End ranging
       pinMode(pingPin, INPUT);           // Set pin to INPUT
       duration = pulseIn(pingPin, HIGH); // Read echo pulse
       inches = duration / 74 / 2;        // Convert to inches
       Serial.println(inches);            // Display result
       delay(200);                     // Short delay }
    

    I found this http://learn.parallax.com/kickstart/28015 as well. I use these sensors on all of my robots and this code
    here is pretty much all you need to get something out of them.

    I hope this helps you out.

    JT
Sign In or Register to comment.