Hello everyone.
Has any of you ever read random characters "(òçöòôg£òôòâôg²«²ððö’ðg’’²ðæô²gðæg“²gò²²ðâà²ð«ôôò£òôgg’", while trying to read data from HC-SR04 Sensor?
I'm pretty sure I've made the connections correctly, since I am only trying to use the example sketch from NewPing library.
The wiring:
VCC -> 5V
Trig -> 12
Echo-> 11
GND -> GND
The code I am trying to use:
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(sonar.convert_cm(uS)); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
Serial.println("cm");
}
And, once again, the reading:
²“’òâ«ô’£²²ðæ²ðà’“’ò²²ðgò’ò²àô²’ôg’’²««ògô’ô’gòâæg’²g²’²«òæggòòàòôgç’ಒòòôg’ò²’æò²²ðgâöôðæ²òô’’«òôð²ðòò²ô²«àô’æg’ðg’g«ô²ð’æ«âಒà²ôôgàògà²ðæô’ô’ððࣲðg£²ôòg’òðgòಒ«òôಒ²’ðð«æô’’²ðæ²ô櫲ôgg’ò²’gࣲgð«òg’«òôgæ²²²«ò²g’ಒòââöðgò²’g£²ô²æ«òôg’«²ðôg’«’ô²ðð’æà²’ô’’«òâ’gò²ö£²ðægòò²ð櫲ðgòg«ô’²’ðæ²æò²’gàôòô“’²²òg²²ò“ò£òðð’ô’ô𫲲ðgð’òô“’£«òôggæâòæðæ’²ð࣓gò²ôgg’òâ²ðâòâgôgög’ö²ô’’«ô²’[...]and so on[...]
Thanks for your reply, it worked on character reading.
However, now the sensor only displays: Ping: 0cm, it doesn't get any distance at all.
Is it possible that the sensor is bad, and I'm going to need a new sensor? Thanks.
It is more likely that you do not have the sensor connected correctly. Time to post a diagram showing how it is connected. Pencil and paper is fine.
Hello, sorry for my late reply.
I made a scheme using a website, but cannot find the UltraSonic sensor (the HC-SR04 sensor, they have the Parallax sensor with 3 pins) on their component list, but I can explain how I mounted the sensor on the breadboard.
I have some alternate code that works with my hcsr-04 ultrasound sensor
This doesn't use a class but it does use interrupts
For UNO or any Atmega328P
Vcc -- 5V
Gnd -- Gnd
Trig -- Pin 4
Echo -- Pin 3 (interrupt 1)
/*
pin 3 = Ping Echo *** The arduino has an easy interrupt on pin 3 that will allow us to simplify my code
pin 4 = Ping trigger
*/
#define TriggerPin 4
// echo pin will be interrupt 1 on pin 3
#define DelayBetweenPings 50 // it works to about 5 milliseconds between pings
volatile unsigned long PingTime;
volatile unsigned long edgeTime;
volatile uint8_t PCintLast;
int PinMask = B1000; // pin 3
float Measurements = 0;
void PintTimer( )
{
uint8_t pin;
static unsigned long cTime;
cTime = micros(); // micros() return a uint32_t
pin = PIND >> 3 & 1; // Quickly get the state of pin 3
if (pin)edgeTime = cTime; //Pulse went HIGH store the start time
else { // Pulse Went low calculate the duratoin
PingTime = cTime - edgeTime; // Calculate the change in time
}
}
void debug()
{
char S[20];
static unsigned long PingTimer;
if ((unsigned long)(millis() - PingTimer) >= 100) {
PingTimer = millis();
Serial.print(dtostrf(Measurements, 6, 1, S));
Serial.println();
}
}
float microsecondsToInches(long microseconds)
{
return (float) microseconds / 74 / 2;
}
float microsecondsToCentimeters(long microseconds)
{
return (float)microseconds / 29 / 2;
}
void PingTrigger(int Pin)
{
digitalWrite(Pin, LOW);
delayMicroseconds(1);
digitalWrite(Pin, HIGH); // Trigger another pulse
delayMicroseconds(10);
digitalWrite(Pin, LOW);
}
void PingIt()
{
unsigned long PT;
static unsigned long PingTimer;
if ((unsigned long)(millis() - PingTimer) >= DelayBetweenPings) {
PingTimer = millis();
cli (); // clear interrupts flag
PT = PingTime;
sei (); // set interrupts flag
Measurements = (float) (microsecondsToCentimeters(PT));
// Measurements = (float) (microsecondsToInches(PT));
PingTrigger(TriggerPin); // Send another ping
}
}
void setup()
{
Serial.begin(115200);
Serial.println("Ping Test");
pinMode(3, INPUT);
pinMode(4, OUTPUT);
attachInterrupt(1, PintTimer, CHANGE );
}
void loop()
{
PingIt(); // Manage ping data
debug();
}
This is Tested 100% and I can get reading down to 1.9cm (haven't Verified accuracy)
This pings every 50 ms but it only shows samples every 100 ms, It can ping as fast as 5ms if you need the resolution.
Let me know if this works for you
Z
zhomeslice:
Hasty simplification of an advanced 8 sensor version... Rename as you wish. Gust offering an alternative non blocking way to use the ultrasound sensor
Oh, darn. I was hoping you were going to buy me a pint (sensor).