Arduino - Ultrasonic sensor (SR04T) measure only 0 - arduino

I have a ultrasonic sensor (SR04T) that I have connected to my Arduino. I'm using the TX and RX port at the Arduino UNO. The problem is that it only reads value 0 cm. Could anyone help me find the error?
The code I'm using looks like this:
const int trigPin = 1;
const int echoPin = 0;
void setup() {
Serial.begin(9600);
}
void loop()
{
long duration, inches, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert time into cm
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}

You are doing two things incorrectly here.
You are setting pinMode in the Loop. No need for that. Put them in the Setup. You don't need to set pinmode continuously.
You are using pin 0 and 1 for your input and output while using Serial. Serial uses pin 0 and 1 for serial communication. Use other pins for your input and output. Have a look at http://marcusjenkins.com/wp-content/uploads/2014/06/ARDUINO_V2.png

Related

Program only works when connected to PC

new to arduino programming, making a proximity sensor with an HC-SRO4 module. It works almost flawlessly when connected to my computer via USB, but whenever I connect it to the wall, it stops working. What I've noticed is that the "TX" light also isn't on when it's connected to the wall, but it is when connected to the PC. The adapter i'm using when it's plugged into the wall is 9V/1A DC that came with the arduino.
void setup() {
#define LED 8
#define trigPin 12
#define echoPin 13
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LED, OUTPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1
;if (distance >= 60 || distance <= 0){
Serial.println("no object detected");
digitalWrite(LED, LOW);}
else {
Serial.println("object detected");
digitalWrite(LED, HIGH);
}}
There is a bug where the serial command suche as serial.begin, serial.write and serial.read if they are meant to for debug purposes on your cpu have a blocking effect on your program. I've experienced it once or twice so try commenting those lines out.

Ultrasonic program giving me an odd error

I created a program that is intended to turn a servo motor 180 degrees then back if an object is within 20cm of the ultrasonic sensor. This works fine except sometimes when the if condition is not met. In this case, the motor doesn't move at all and the distance is put into the serial monitor. This works fine sometimes, but in most cases the serial monitor just prints 0 repeatedly and I have to restart the program for it to work again. Even when I put an object within 20cm of the sensor it still prints 0. I am quite new to Arduino so this really just stumped me haha. Any tips or help would be greatly appreciated.
#include <Servo.h>
Servo servo1; // create servo object to control a servo
#define trigPin 13
#define echoPin 12
void setup()
{
Serial.begin(9600);
servo1.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
long duration = 0, distance = 0;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
if(distance > 2 && distance < 20){
servo1.write(180);
Serial.println(distance);
delay(1000);
servo1.write(-180);
}
else{
servo1.write(0);
Serial.println(distance);
}
delay(2000);
}
See what happens when you try this code, as I cleaned it up a little bit:
#include <Servo.h>
#define trigPin 13
#define echoPin 12
Servo servo1; // create servo object to control a servo
void setup() {
Serial.begin (9600);
servo1.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
float duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 29.1;
if (duration) {
if (distance > 2 && distance < 20) {
servo1.write(180);
Serial.println(distance);
delay(1000);
servo1.write(-180);
}
else {
servo1.write(0);
Serial.println(distance);
}
}
delay(2000);
}
I think if you use a float it might work. However, if it doesn't work, try switching up the pin assignments to different pins on the Arduino and see if that works. If the distance measurment is off the best way to adjust it is with the multiplied value in the distance variable in the void loop ().This one works for me.

SoftwareSerial Bluetooth writing empty strings?

Im using an arduino and an hc-06 to communicate with an android. I'm trying to send int values over, but the android says it's receiving empty strings. This is my code:
#include <SoftwareSerial.h>
#define rxPin 11 // define SoftwareSerial rx data pin
#define txPin 10 // define SoftwareSerial tx data pin
#define trigPin 5
#define echoPin 6
SoftwareSerial blueTooth(rxPin, txPin);
void setup() {
Serial.begin (9600);
blueTooth.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29;
Serial.println(distance);
blueTooth.print ((int)(Serial.read()));
delay(200);
}
I think what you want is
blueTooth.print(distance);
First suggestion, try switching the rx and tx pins. I know I've messed that up a bunch. If you didn't know, the rx from the hc-06 should be defined as the tx pin in software serial and visa versa. Michael Yu's comment is definitely preferred to what you originally had. The other thing I can suggest is posting the android code that receives and parses the incoming data.
My string sending function for arduino:
void sendStr(String str){
char b[2];
for(int i = 0; i < str.length(); i++){
String dataSend = str.substring(i,i+1);
dataSend.toCharArray(b,2);
bluetooth.print(b);
delay(1);
}
}

Arduino Ultra Sonic Sensor always returns 0

I am doing a basic project in Arduino UNO connecting an Ultra Sonic sensor (HC-SR04) which should print in the serial monitor the distance of the closest object but it always print 0.
This is my code:
long distance;
long time;
void setup(){
Serial.begin(9600);
pinMode(4, OUTPUT);
pinMode(2, INPUT);
}
void loop(){
digitalWrite(2,LOW);
delayMicroseconds(5);
digitalWrite(2, HIGH);
delayMicroseconds(10);
time = pulseIn(4, HIGH);
distance = int(0.017*time);
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm.");
delay(1000);
}
And this is the breadboard:
The primary issue that I see is that your code doesn't match your wiring diagram.
For example, your diagram shows Trig connected to pin 4. The Trig should be the output from your Arduino but you have it defined as an input.
The Echo is connected to pin 2 and it should be an input, but you have it defined as an output.
Finally, in your loop(), you are not even using pin 2 or pin 4, but pins 9 and 8. Another issue is the timing you use in setting the trigger pulse - it does not match the datasheet. I would do something like this (assuming that you are actually connected to the pins shown in your diagram):
#define sensorTrigPin 4
#define sensorEchoPin 2
void setup()
{
Serial.begin(9600);
pinMode(sensorTrigPin, OUTPUT);
pinMode(sensorEchoPin, INPUT);
}
void loop()
{
int pulseWidth = 0;
digitalWrite(sensorTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(sensorTrigPin, LOW);
pulseWidth = pulseIn(sensorEchoPin, HIGH);
Serial.print("Pulse Width: ");
Serial.print(pulseWidth);
delay(1000);
}
Note that pulseWidth is just the amount of time that it takes from the beginning of the Echo pulse going high to the end of the same pulse (when it goes low). You would still have to calculate the distance based on the value of pulseWidth.
UPDATE BASED ON RECENT EDIT TO THE QUESTION
If you change a portion of your loop() code to this, it should work:
void loop(){
digitalWrite(4, HIGH); //was (2, LOW)
delayMicroseconds(10); //was (5)
digitalWrite(4, LOW); //was (2, HIGH)
//REMOVED EXTRA DELAY
time = pulseIn(2, HIGH); //was (4,HIGH);
... //Keep the rest of your code the same.
}
Try connecting your VCC of the sensor to 3V3 instead of 5V. This might sound odd, but I tried it and it worked well. Also, please make sure that your echo and trig pin match the code.

arduino Ultrasound srf05 wrong value

My Arduino board is Arduino Due ATmega328p. My Ultrasound is srf05.
I measure the distance is 5cm but my serial show me "531".Serial Monitor show the int of distance which value is always be 531.Why?
Here are the codes.
#define ECHOPIN 2 // Pin to receive echo pulse
#define TRIGPIN 3 // Pin to send trigger pulse
void setup(){
Serial.begin(9600);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
}
void loop(){
digitalWrite(TRIGPIN, LOW); // Set the trigger pin to low for 2uS
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW); // Send pin low again
int distance = pulseIn(ECHOPIN, HIGH); // Read in times pulse
distance = distance/58; // Calculate distance from time of pulse
Serial.println(distance);
delay(50); // Wait 50mS before next ranging
}
The srf05 times out if it doesn't receive the echo pulse. The width of the pulse is 30 ms.
30,000 / 58 is 517, which is close to 531. It looks like your device is not receiving an echo from the object. This makes your problem a hardware issue, and not a programming issue. (The code looks correct.)

Resources