1

I am learning Arduino and pardon me if this is nooby. I am trying to make an LED glow when the tilt switch is tilted. The LED should not glow if the tilt is in normal position. The circuit and the code are in the attached image.

Based on my understanding of the code, I thought that when the tilt switch pin 5 is passed with HIGH signal from 5V the IF condition gets satisfied and tje LED on pin 13 would glow.

But it does not happen. The LED just glows before I tilt the tilt switch. Could you please clarify/correct my mistake. Thank you.

Circuit

Attaching the code below as per suggestion

int ledPin = 13;
int switchPin = 5;

void setup() {
  pinMode(ledPin,OUTPUT);
  pinMode(switchPin,INPUT);  
}

void loop() {
  Serial.begin(9600);
  if (digitalRead(switchPin) == HIGH) {
    digitalWrite(ledPin,HIGH);
    Serial.print("we are here");
  }
  if (digitalRead(switchPin) == LOW) {
    digitalWrite(ledPin,LOW);
    Serial.print("there");
  }
}
dda
  • 1,553
  • 1
  • 12
  • 17
  • 1
    Don't paste an image of the code. Code is text -- just paste the text right into the question, select it and type CTRL-K to format it as code. – jose can u c Mar 30 '18 at 21:13
  • int ledPin = 13; int switchPin = 5; void setup() { pinMode(ledPin,OUTPUT); pinMode(switchPin,INPUT); } void loop() { Serial.begin(9600); if (digitalRead(switchPin) == HIGH) { digitalWrite(ledPin,HIGH); Serial.print("we are here"); } if (digitalRead(switchPin) == LOW) { digitalWrite(ledPin,LOW); Serial.print("there"); } } – user2823393 Mar 30 '18 at 21:15
  • 1
    Edit your question -- don't put the code as a comment. – jose can u c Mar 30 '18 at 21:16
  • Check your tilt switch with the continuity setting on a multimeter to find out at what orientations it's considered "ON" and which are "OFF" – jose can u c Mar 30 '18 at 21:17
  • Did that Sir. Just figured it out :) – user2823393 Mar 30 '18 at 21:17
  • Thanks for the response, Even when I flick it ON/OFF the LED keeps glowing, I think I maybe making mistake on the pinmodes or something :S If the orientation was the issue, then the other IF condition would have satisfied when I tilt the switch – user2823393 Mar 30 '18 at 21:19
  • The code is functional, besides the fact that you're probably filling up the serial port output buffer. Note that your `loop()` runs at probably close to half a million times a second, and you are trying to write a string of text every time through the loop. Try commenting out the `Serial.print()` statements and see if that clears anything up. – jose can u c Mar 30 '18 at 21:25
  • 1
    How about using a pullup/pulldown resistor? When there is no connection from the input pin to any potential it will be floating – chrisl Mar 30 '18 at 22:13
  • Thanks chrisl, Its what you said. The PULLUP. Looks like I did not read the theory right. Switches are supposed to be connected to the ground, with PULLUP specified. I connected them to 5V. – user2823393 Mar 30 '18 at 22:43
  • Learning arduino with a tinkercad simulation? Do you have a real arduino board? – Jot Mar 30 '18 at 23:51
  • Yes I do have it. But I am a noobie and little paranoid that I might short it. So I learn using tinkercad and if it works then I try them out on Arduino – user2823393 Mar 31 '18 at 06:31
  • Is this a simple mercury switch? You call it a tilt switch but I am not sure what you mean. – SDsolar Apr 05 '18 at 02:12

1 Answers1

1

Understanding how PULLUP works lead to the solution. Also learnt that switches are supposed to be connected to ground. Thank you all for your time. Especially chrisl.

enter image description here

  • Switches aren't "supposed" to be connected to ground. It's just that if they are you can make use of the internal pullup instead of having to attach an external pulldown. – Majenko Mar 31 '18 at 10:45
  • Don't post code as an image. – gre_gor Apr 03 '18 at 18:42