Hello guys,
I have an ultrasonic range sensor HC-SR04, I made a prototipe with an Arduino duemilanove, which worked correctly.
The problem is, when I switch the sensor to my arduino Nano, it reacts differently. I start to read ‘0’ cm most of the time, and it just read the correct distance when the surface in front of the sensor is flat and perpendicular (90º) with the sensor.
With the Duemilanove, it reads almost any surface and support a greater range on angles and never displays ‘0’. Sometimes display huge distances but never ‘0’.
For example, with duemilanove I can read the dist to my t-shirt while with nano it just read the wall or flat surfaces.
The code is absolutely the same and for anything else, the Nano board is working alright.
Any clues? help
**The code which works well with duemilanove but not with Nano:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define echo A5
#define trig A4
unsigned long duration;
void setup() {
lcd.begin(16, 2);
pinMode(echo,INPUT);
pinMode(trig,OUTPUT);
pinMode(7,OUTPUT);
digitalWrite(7,HIGH);
Serial.begin(9600);
}
void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo,HIGH);
Serial.println(duration/59,DEC);
lcd.setCursor(0,0);
lcd.print(duration/59,DEC);
lcd.print(" ");
delay(500);
}
Moderator edit: code tags added