Arduino ultrasonic initialisation issue - initialization

Hardware:
Arduino MEGA 2560
2 x MaxBotix MaxSonar-EZ0
Software (relating to Ultrasonics, by no means the entire program):
void setup() {
Serial.begin(9600);
//Ultrasonic Left
pinMode(26, OUTPUT);
pinMode(2, INPUT);
digitalWrite(26, LOW);
//Ultrasonic Right
pinMode(27, OUTPUT);
pinMode(3, INPUT);
digitalWrite(27, LOW);
}
void readSonar() {
digitalWrite(26, HIGH);
delayMicroseconds(25);
digitalWrite(26, LOW);
data[0] = pulseIn(2, HIGH);
digitalWrite(27, HIGH);
delayMicroseconds(25);
digitalWrite(27, LOW);
data[1] = pulseIn(3, HIGH);
return data;
}
Problem:
When the Arduino is first booted, the readings from the two Ultrasonic sensors are not being updated. They are reporting as non-zero values, typically in the range of 500 - 1500. They fluctuate a little (most likely due to noise in the power supply), but tend to stay around the value that they initialise to.
As per the data sheet for these sensors, there are no obstacles within 14 inches of the sensors during the initialisation stage.
By simply disconnecting and reconnecting the cable going to the sensors (from the back of the sensor, not directly to the Arduino inputs), I am able to receive accurate readings from the sensors immediately.
Has anyone had this problem before? My setup() function looks 'normal' from the examples that I have seen. In order to fix this problem, I have connected a switch for the active lines of both sensors. This way the Arduino can boot and then I can give the sensors power. This seems like a botched workaround to me, and I would like a hard-coded software solution, if anyone is able to provide one!

If you look at the characteristics of the sensor it looks clear that you need to start them with the Rx at 0. Look at this link. This is most probably the reason why you have to disconnect the sensor after you start the Arduino to get it working. You also need to make sure that you have pullup resistors connected to avoid unreliability of the readings.

Related

I set my pushbutton switch up incorrectly

I am fiddling with an Arduino Uno and trying to get two switches to operate two servo motors. I am not able to do this right now, and I am not sure what my issue is. I know that my servos are wired correctly; however, I highly doubt that my switches are correctly wired.
Here is a diagram of my circuit and the code that I am using.
#include <Servo.h>
Servo servo_11;
Servo servo_10;
void setup()
{
servo_11.attach(11);
servo_10.attach(10);
pinMode(A3, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A2, INPUT);
}
void loop()
{
servo_11.write(0);
servo_10.write(0);
digitalWrite(A3, LOW);
digitalWrite(A2, LOW);
if (analogRead(A2) != LOW) {
servo_11.write(180);
}
delay(10);
}
Circuit image
If anyone could point out what's wrong, I would really appreciate it!!
You have floating inputs. When the button isn't pressed the pin isn't connected to anything. So the pin can read whatever noise it happens to pick up from the environment. You need pull-down or pull-up resistors. If you use pull-ups, then you can use the ones built into the chip.
You also seem to be confusing analogRead and digitalRead. It's digitalRead that gets HIGH and LOW. analogRead gets values from 0 to 1023 for 0 to 5V.
pull-down resistor circuit
try looking on this to understand the concept
it makes sure that when you dont press the pin will read ground and only when it presses the button you make voltage divider that ups the volage to a non-zero to give you HIGH

Using Serial.print and digitalWrite in Same Arduino Script

I am using an Arduino Uno and Windows 7. My goal is to have an LED light that blinks, and when it blinks, it prints out "Blink" to the serial monitor.
When I run the code below, I am able to print out "Blink" to the serial monitor every 2 seconds, however, the light remains constantly on. When I remove the line
Serial.begin(9600);
the lights will blink, but nothing will print. The code I am running is as follows:
int LED3 = 0;
void setup() {
// When I comment out the line below, the lights flash as intended, but
// nothing prints. When I have the line below not commented out,
// printing works, but the lights are always on (ie do not blink).
Serial.begin(9600); // This is the line in question.
pinMode (LED3, OUTPUT);
}
void loop() {
Serial.println("Blink");
digitalWrite (LED3, HIGH);
delay(1000);
digitalWrite (LED3, LOW);
delay(1000);
}
I am unclear on what causes this behavior and would appreciate an explanation of why this occurs and how to avoid this problem. Thank you!
what causes this behavior ?
Pins 0 and 1 are used for serial communications. It's really not possible to use pins 0 and 1 for external circuitry and still be able to utilize serial communications or to upload new sketches to the board.
From the Arduino Serial reference documentation:
Serial is used for communication between the Arduino board and a computer or other devices. All Arduino boards have at least one serial port (also known as a UART or USART): Serial. It communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via USB. Thus, if you use them in functions in your sketch, you cannot also use pins 0 and 1 for digital input or output.
Just think it this way how can a pin act serial and digital simultaneously ? Yeah that's what you are trying to do !! . You set pin to serial at a baud rate and then you used it for making LED blink.
So, when you do serial.begin(9600); it Sets the data rate in bits per second (baud) for serial data transmission to 9600.Thus you used serial pins in this function, after that you cannot use pins 0 and 1 for digital input or output ( like an LED ). when you comment serial.begin(9600); your pins are free to use and thus you obtain the output.
how to avoid this problem ?
Change the LED from pin 0 to digital pins.
The following code will obtain the result that you expect (I used pin 7 in it) :
int LED3 = 7; //I have changed pin to 7 ( you can use any except 0 and 1 )
void setup() {
// When I comment out the line below, the lights flash as intended, but
// nothing prints. When I have the line below not commented out,
// printing works, but the lights are always on (ie do not blink).
Serial.begin(9600); // This is the line in question.
pinMode (LED3, OUTPUT);
}
void loop() {
Serial.println("Blink");
digitalWrite (LED3, HIGH);
delay(1000);
digitalWrite (LED3, LOW);
delay(1000);
}
The Arduino is using pins 0 and 1 for serial communications.
Pin 0 is RX and 1 is TX, so when you are trying to flash the LED also connected to pin 0, the two start stomping all over each other.
Try to move the LED to a different pin and update your sketch to match, and it should start working.
This page contains information on the Arduino serial connection: https://www.arduino.cc/en/Reference/Serial
Happy hacking

Robot won't move any further after a few wheel turns

I'm building a robot for a school project. I am currently using an Arduino Uno, two DC motors and an ultrasonic sensor. The two motors are being controlled via the Arduino Motor Shield v3. I want the robot to be autonomous so it has to be able to move around on its own using the ultrasonic sensor.
This is the latest version of my source code:
#include <Servo.h> // include Servo library
#include <AFMotor.h> // include DC motor Library
#define trigPin 12 // define the pins of your sensor
#define echoPin 13
AF_DCMotor motor2(7); // set up motors.
AF_DCMotor motor1(6);
void setup() {
Serial.begin(9600); // begin serial communication
Serial.println("Motor test!");
pinMode(trigPin, OUTPUT); // set the trig pin to output to send sound waves
pinMode(echoPin, INPUT); // set the echo pin to input to receive sound waves
motor1.setSpeed(105); // set the speed of the motors, between 0-255
motor2.setSpeed (105);
}
void loop() {
long duration, distance; // start the scan
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // delays are required for a successful sensor operation.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // this delay is required as well!
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1; // convert the distance to centimetres.
// if there's an obstacle ahead at less than 25 centimetres, do the following:
if (distance < 25) {
Serial.println("Close Obstacle detected!" );
Serial.println("Obstacle Details:");
Serial.print("Distance From Robot is " );
Serial.print(distance);
Serial.print(" CM!"); // print out the distance in centimeters.
Serial.println (" The obstacle is declared a threat due to close distance. ");
Serial.println (" Turning !");
motor1.run(FORWARD); // Turn as long as there's an obstacle ahead.
motor2.run (BACKWARD);
} else {
Serial.println("No obstacle detected. going forward");
delay(15);
motor1.run(FORWARD); // if there's no obstacle ahead, Go Forward!
motor2.run(FORWARD);
}
}
The current issue is that the wheels are rotating as expected but after a few turns they stop.
I suspect that the issue is software related, but I am not completely sure. Moreover, I believe that the motors are correctly connected to the motor shield, but I might not handle them properly in the code.
Could anyone please help me solving this issue?
Simple answer - get rid of the delay in the loop
delay(15);
in combination with the used libraries this leads after some time to no motion at all. Delay stops processing everything, so use a nonblocking time measurement to give the motor routines the processing time instead of blocking everything. See blinkwithoutdelay example in ArduinoIDE how to implement this kind of routine. When working with robots always look into the libs (= advantage of open source) if the author uses delay() If yes either rewrite the function or ditch the lib. Especially in Arduino environment a lot of really bad libs are around - they work in simple testing cases on the desktop, but not in complex close to real time environments like robotics.
I think you must use PWM pins for motors. Since AFMotor library has option to set speed. So please change pin 7 to pin 5 for motor2 and check.

Arduino UNO + Ethernet Shield + Ultrasonic Sensor = Fail

With my Arduino Uno I measure the distance using HC-SR04 ultrasonic sensor with no problems at all using the wiring below.
When I attach ethernet shield, my ultrasonic sensor does not measure distance any more, it constantly says 0cm no matter what. I have tried different digital pin pairs such as 5-7, 6-8, 5-9, 3-5, 2-8 but no luck.
I suspect that HC-SR04 is not compatible with my Ethernet shield but I haven't seen such warning anywhere on the net.
There are no components attached to arduino besides ethernet shield and the ultrasonic sensor itself.
There is no SD Card in SD Card slot.
My ethernet shield works fine while running a web server or web client script.
Digital pins of ethernet shield works fine with all other components such as temperature sensor, motion sensor etc.
Here is the ethernet shield I have;
http://www.ezshopfun.com/product_info.php?products_id=169
Here is my actual circuit;
http://s7.postimg.org/vyi2z36qz/20140826_001130.jpg
http://s7.postimg.org/6eb7ewvzf/20140826_001150.jpg
http://s7.postimg.org/6psnrocff/20140826_001156.jpg
http://s7.postimg.org/y6ro2ooh7/20140826_001229.jpg
http://s7.postimg.org/71a44fsvf/20140826_001247.jpg
Here is my code;
#define trigPin 6
#define echoPin 7
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH) / 2;
distance = duration / 29.1;
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Today I bought a multimeter and tested my circuit. Here are the results;
When my circuit directly attached to Arduino itself;
4.80V & 5.7mA
When my circuit attached to ethernet shield;
3.06V & 3.8mA
I think the problem is that 3.06V is not enough for my HC-SR04 to operate.
Yeah based on this photo
you're not grounding your sensor. You have two power supplies going into it. This, needless to say, is bad for a number of reasons. First and foremost because it wont work ungrounded lol
As others have said, it looks like the main issue is that you need this connected to 5V and check your wiring generally.
However, there is another potential issue:
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
You only need to set the trigger pin high for 10 microseconds, not 1000 microseconds. I don't know if this is an issue or not but there is no need to wait this long. You could potentially be missing some or all of the incoming pulse by waiting that long.
You might want to checkout out some HC-SR04 tutorials too, like this one:
http://superawesomerobots.com/tutorials/hc-sr04-tutorial-for-arduino/
Hope that helps.
I had a similar problem with a wifi shield + ultrasonic sensor. I found switching from pins 13(trig) and 11(echo) to 8(trig) and 3(echo) fixed it.
See here: http://forum.arduino.cc/index.php?topic=201827.0

Reading sensor input with Pic32 and MPIDE

So I currently have a pic32 arduino. I'm pretty new to this stuff, so any tips would be appreciated.
I have a sensor that has 3 pins, 5VDC, ground, and sensor output. I connected the sensor output and ground header to the two pin slots at PORT0.
For some reason, the program always reads that the sensor is HIGH, even if the sensor is not connected.
If I connect the output to a breadboard with an LED, I can see the LED toggle on and off.
Here is my code:
const int sensor = 0; //sensor port
int sensorState = LOW;
void setup(){
pinMode(ledPin, OUTPUT);
pinMode(piezo, OUTPUT);
pinMode(sensor, INPUT);
Serial.begin(9600);
}
void loop(){
sensorState = digitalRead(sensor);
if(sensorState == HIGH)
alarm();
digitalWrite(ledPin, sensorState);
Serial.println(sensorState);
}
You may have the internal pull-up resistor enabled, so when nothing is connected, it will read high.
Also, these two statements are contradictory:
For some reason, the program always reads that the sensor is HIGH, even if the sensor is not connected.
If I connect the output to a breadboard with an LED, I can see the LED toggle on and off.
So the program always reads high but the LED toggles on or off? Which one is it?
If you manually pull the pin to ground, does your program react the way it is supposed to? If it does, then you should take a look at your sensor circuit.
Your sensor circuit sounds weird - you say
I have a sensor that has 3 pins, 5VDC, ground, and sensor output. I connected the sensor output and ground header to the two pin slots at PORT0
So the sensor output and ground are connected to pin zero? 5v should go to 5v, ground should go to ground, the sensor output should go to pin zero.

Resources