Hi all!
First off, I'm completely new to Arduino, electronics, and the forum so this might be a simple answer...
Goal:
I'm trying to build a circuit with a tilt switch in it. When the switch is closed, I want the arduino to recognize it and turn on an LED.
Problem:
The circuit works, sort of. For awhile the LED was flashing for no apparent reason with the tilt switch in the open position. I disconnected the tilt switch section from the circuit completely and the LED would still flash. I discovered that if I grabbed a wire connected to a ground pin the flashing would stop and the tilt switch would operate somewhat normally. I say somewhat because it takes a few seconds for the arduino the recognize that the switch is open again when I tip it back.
EDIT: Oh also, if I bring my hand closer to the LED while not touching a ground wire, the flashing intensifies. If I move away from the LED, it calms down and stops.
Question:
What's going on? Is the arduino so sensitive that it's reading voltage from my body? I'm assuming that I'm doing something wrong here...
Set Up:
5V -> 3300? Pull up Resistor -> Tilt Switch -> Pin 12
Pin 4 -> 2200? Pull up Resistor -> Tiny lil green LED -> Ground
EDIT: Schematic Image Attached
Code:
#define led 4
int input = 12;
int count = 0;
int val = 0;
void setup () {
Serial.begin(9600);
pinMode(led,OUTPUT);
pinMode(input, INPUT);
}
void loop() {
val = digitalRead(input);
if (val == HIGH) {
digitalWrite(led, HIGH);
Serial.println(val);
}
else {
digitalWrite(led, LOW);
Serial.println(val);
}
}