Ultrasonic range sensor not working correctly with Nano

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 :frowning:

**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);

void loop() {
  digitalWrite(trig, LOW);
  digitalWrite(trig, HIGH);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo,HIGH);
  lcd.print("  ");

Moderator edit: code tags added

Almost sure this is not a programming question, but anyway.

Also noted that it can't read ranges above 2m with Nano, while with duemilanove it works correctly.

Thanks for any help,

Could it be a voltage/power issue?

I came across this issue with my recent project. The issue was actually happening only when I power the nano from USB. This is a Power issue. I powered Nano with a battery (6 - 9v is enough) and it started work fine.