ARDUINO pin is constantly changing from HIGH to LOW? - arduino

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.

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

cannot turn on relay indefinitely on an arduino

I want to make a device like Knocki(https://knocki.com), which essentially is a relay control using a vibration sensor. i can detect vibrations rn but the problem is, once i knock the relay blinks on and then turns off. i understand this is a lack of programming that is causing this. could someone help me write code which makes it so that when i knock the relay is turned on indefinitely; until I knock again to turn relay off.
And yes u can probably tell that this code is copied from somewhere(https://wiki.keyestudio.com/Ks0068_keyestudio_37_in_1_Sensor_Kit_for_Arduino_Starters#Project_21:_Vibration_Sensor).I took it from the home page of the vibration sensor. the code was initially so that every time i knocked, the onboard Arduino led lit up. Also, right now the relay is blinking faintly every time i knock(Although correctly,in sync with my knocks)
#define SensorLED 13
#define SensorINPUT 3 //Connect the sensor to digital Pin 3 which is Interrupts 1.
unsigned char state = 0;
int Relay = 5;
void setup()
{
pinMode(SensorLED, OUTPUT);
pinMode(SensorINPUT, INPUT);
attachInterrupt(1, blink, FALLING);// Trigger the blink function when the falling edge is detected
}
void loop()
{ if(state!=0)
{
state = 0;
digitalWrite(SensorLED,HIGH);
delay(500);
digitalWrite(Relay,HIGH);
}
else
digitalWrite(SensorLED,LOW);
digitalWrite(Relay,lOW);
}
void blink()//Interrupts function
{ state++;
Yes its in your code: The (bad) example works only because there is a
digitalWrite(SensorLED,HIGH);
->>> delay(500);
a delay for 1/2 sec to keep the led on.So as a check put an other delay after the relay line and it should go on for 1/2 sec too (so the led is lit 1 sec in total)
digitalWrite(SensorLED,HIGH);
delay(500);
digitalWrite(Relay,HIGH);
delay(500);
Thats just for checking -> NEXT STEP:
Get rid of the delays (see blinkwithoutdelay example in
Arduino->File->Examples->2.Digital -> blinkwithoutdelay
and introduce a second state variable e.g.
bool relayStateOn = false;
to get an independent on/off of the relay and the led.(If thats - what I understand -what you want to do)
If you feed your relay from the board, that is not the problem. Please, check the voltage in your relay when you try to set it on, if your voltage falls down, it means that this output to your relay does not supply the necessary current.

Concerning HC-SR04's capability in measuring distance

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.

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

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