Concerning HC-SR04's capability in measuring distance - arduino

I am currently working with the HC-SR04 ultrasonic sensor to measure the distance from the sensor to the surface.
Problem is that the resulting value from the sensor is neither consistent nor accurate within reason.
I read that the HC-SR04 has a maximum reading range of up to 400cm.
My current situation is that the sensor can reasonably measure up to 20 cm, but when it exceeds that it rapidly fails.
For example, the distance between the sensor and surface is approximately 170cm, but the sensor says it is approximately 50cm.
Here is how pins are connected.
HC-SR04------Arduino.
Trig to 13.
Echo to 12
Vcc to 5V
GND to GND
Here is my code for Arduino.
#define echoPin1 12
#define trigPin1 13
float duration;
float distance1_1;
void setup()
{
pinMode (echoPin1, INPUT);
pinMode (trigPin1, OUTPUT);
digitalWrite(trigPin1, LOW);
Serial.begin (9600);
Serial.println("Program Begins");
}
void loop()
{
DIST();
delay (1000);
}
void DIST()
{
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration=pulseIn(echoPin1, HIGH);
distance1_1=(duration*0.0343)/2;
Serial.println("Distance")
Serial.println(distance1_1);
}

Try using different Arduino pins for connecting the echo and trigger pins. The pin 13 of Arduino is connected to the LED and there are some reports of problems while using pin13 for the ultrasonic sensor on certain Arduino models/revisions. So first try changing the pins.
Next, try a spare sensor (if available) and see if the results are consistent. Sometimes the problem is with the broken ultrasonic sensor.
Also, we need to know the exact hardware setput i.e. how are you mounting the sensor. There could be stray reflections from the nearby objects so need to be careful while mounting the sensor.
Finally, the return type of pulseIn is unsigned long. So you should
store the value from pulseIn in a long datatype, not float.

Related

Arduino - Light is staying on for more than expected duration

Code is suppose to Light the inbuit-LED at Pin 13 whenever Pin 5 is High, however i have encountered couple of problem.
While measuring Voltage though Digital Meter - one pin at arduino GND and other at 1,2,3,4. They are showing some non-zero values. Earlier triggering Pin was 4 and light was staying on all the time.
When Pin 5 is high (by connecting 5V Pin from Arduino to Pin 5) it lights the LED as it should, but if Pin 5 stays high for more than 1/2 second, light stays high for more than 0.5 second even after the Pin 5 is disconnected from 5V Pin.
int buttonState = LOW;
int light = 13;
void setup() {
// put your setup code here, to run once:
pinMode(gateopen,INPUT);
pinMode(light, OUTPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
buttonState = digitalRead(gateopen);
if (buttonState == HIGH)
{
digitalWrite(light, HIGH);
}
else
{
digitalWrite(light, LOW);
}
//delayMicroseconds(500);
}
As suggested earlier, use a pull down resistor. Or change your circuit to active low and use INPUT_PULLUP.
I believe you're using delayMicroSeconds, which means the led is just blinking waaay too fast for your eyes. If you want 'half a second' as you hinted on your query, you would be using delay(500) instead.
You are experiencing electrical noise, just like Juraj said, just put a 220 Ohm pull down resistor

How to turn motor counter-clockwise

Currently, my code below turns on the motor, delays for a bit then starts again. This is all being done in the clockwise direction, however how can I write my code so it can turn counter-clockwise?
int motorPin = 3;
void setup()
{
pinMode(motorPin, OUTPUT);
}
void loop()
{
startStopMotor(135);
delay(1000);
startStopMotor(0);
delay(1000);
}
void startStopMotor(int speed){
analogWrite(motorPin, speed);
}
By the code you share i'am guessing you are running a low power 5v dc motor... but you should edit your answer to give us what type of hardware you are using. This is not an answer but an idea of what you should be looking for... Basically on the motor i suppose you have, you have pin 1 and pin 2. Pin 1 is connected to a PWM signal and pin 2 is connected to ground. This configuration allows you to run your motor clock'wise. To run your motor counter clock'wise you need to invert the direction of the current basically have pin 1 connected to ground and pin 2 connected to a PWM signal.
Now there are multiple ways of doing this, i am unsure of the exact code to do this on an arduino but your pin 1 and 2 will be connected each to a PWM pin. In the code you will need to tell the arduino to put Pin 1 or 2 as a pullDown pin which basically mimics a ground thus telling the direction the other pin will output a PWM
this is not example code but it will give you an idea of what it should look like
void loop(){
//move clock'wise
pin1.pullup();
pin2.pulldown();
analogWrite(pin1, 180);
//move counterclock'wise
pin2.pullup();
pin1.pulldown();
analogWrite(pin2, 180);
}

ARDUINO pin is constantly changing from HIGH to LOW?

I used this code to check the state of Arduino pin 8.
To see if the pin is High or Low but my output continuously changes from high to low.
I am not connecting anything to pin 8 while running this code.
const int Pin = 8;
int Reading=0;
void setup() {
Serial.begin(9600);
delay(2000);
pinMode(Pin, INPUT);
}
void loop() {
Reading = digitalRead(Pin);
if(Reading == HIGH)
{
Serial.println("HIGH");
delay(2000);
}
if(Reading == LOW)
{
Serial.println("LOW");
delay(2000);
}
}
But my Output Comes like this:
OUTPUT:
HIGH
HIGH
LOW
LOW
HIGH
HIGH
LOW
LOW
HIGH
HIGH
LOW
LOW
HIGH
HIGH
LOW
LOW
Don't know what to do ??
This is the correct behavior.
Since you don't connect the pin the read should be undefined (meaning it's unstable). Check "floating" state to learn more.
If you want to make it stable, consider using the internal pull-up resister. Change the line
pinMode(Pin, INPUT);
to
pinMode(Pin, INPUT_PULLUP);
to make it always HIGH while disconnected. In this case you should consider the internal pull-up resistance when you actually try to connect the pin.
The official Arduino documentation provides more detailed descriptions on each GPIO states.
As the internal pull ups are weak, sometimes adding
pinMode(Pin, INPUT_PULLUP);
won't solve the problem so you need to add a 10K or higher value resistance between pin and ground/power to initially make the pin pull up or pull down.

Motor rotates only one direction with L293D controller

Hello I have an RC car which has two 3v motors (one for left/right and the other one for forward/back). The left and right motor is working fine but when I try to rotate the other motor it rotates only back. I've tried the motor separately and it works in both direction without the controller.
My code is the following:
int enablePinMotorAF = 3;
int in1PinMotorAF = 5;
int in2PinMotorAF = 6;
int enablePinMotorLR = 11;
int in1PinMotorLR = 10;
int in2PinMotorLR = 9;
boolean reverse = true;
void setup() {
pinMode(enablePinMotorAF, OUTPUT);
pinMode(in1PinMotorAF, OUTPUT);
pinMode(in2PinMotorAF, OUTPUT);
pinMode(enablePinMotorLR, OUTPUT);
pinMode(in1PinMotorLR, OUTPUT);
pinMode(in2PinMotorLR, OUTPUT);
}
void loop() {
//go forward ->not working
analogWrite(enablePinMotorAF, 230); //max speed
digitalWrite(in1PinMotorAF, reverse);
digitalWrite(in2PinMotorAF, !reverse);
delay(3000);
//go back -> working
analogWrite(enablePinMotorAF, 230); //max speed
digitalWrite(in1PinMotorAF, !reverse);
digitalWrite(in2PinMotorAF, reverse);
delay(3000);
//go right -> working
analogWrite(enablePinMotorLR, 230); //max speed
digitalWrite(in1PinMotorLR, !reverse);
digitalWrite(in2PinMotorLR, reverse);
delay(3000);
//go left -> working
analogWrite(enablePinMotorLR, 230); //max speed
digitalWrite(in1PinMotorLR, reverse);
digitalWrite(in2PinMotorLR, !reverse);
delay(3000);
}
Here is the wiring too:
Wiring
The green and orange wires are for a Bluetooth module.
Do you have any idea how can I solve this problem and to make it work?
Thank you.
To reverse the motors, you need four pins, two for each motor. On a readily-available L293 module, they are often labeled IN1, IN2, IN3, and IN4.
To make one motor go forward, you might set IN1 to 5V and IN2 to 0V. To reverse it, simply switch the inputs, IN1 to 0V and IN2 to 5V. In this case 5V is a digitalWrite(pin, HIGH).
Similar for the other two pins for the other motor. I start my answer with this because the wiring of what output pins to what input pins is vitally important.
The Enable pins is where you've gone wrong, it seems. Enable2 and Enable1 should be connected to the pins to which you're doing the analogWrite() but enablePinMotorAF = 3 for example connects to a motor signal input, not to Enable2 as it probably should. Start with fixing that... your two pins 3 and 11 should be connected to Enable1 and Enable2. You only need PWM on the Enable pins. The others should simply be activated with digitalWrite().
Once you get the Enable n pins connected to PWM, then you'll have a good PWM enable signal. Simply connect the other pins on the same side of the chip (IN1 and IN2 for Enable1 and IN3 and IN4 for Enable2), and turn them on and off with `digitalWrite(pin, HIGH) and you'll be good to go.

Sodaq - How do you toggle the "switched" groove connectors with a Sodaq Moja

How do you turn the power on and off in software for the "Switched" side of the grove connectors on a Sodaq Moja?
I know its a matter of "turning off the ground". What is an example of code that does that?
I figured out how to do it. While it is really not documented there is an accidental hint under section "2. Programming the SODAQ board" at http://www.sodaq.net/#!getting-started/c21ma.
"Pin 6 is normally used for switching power to the Switched row of Grove connectors, but for now we just use it because it has an LED too."
The digital pin 6 powers both the led and the actual grove connectors as well. Here is some sample code.
void setup()
{
pinMode(6, OUTPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(6, HIGH); // Turns on both the LED and the power
delay(400);
int value = analogRead(A0); // read your analog sensor that needs power
digitalWrite(6, LOW); // turns off the power
Serial.println(value);
delay(1000);
}

Resources