Go Down

Topic: Magnetic Switch or Push button Switch, getting stuck? (Read 1 time) previous topic - next topic


May 10, 2013, 11:34 pm Last Edit: May 10, 2013, 11:37 pm by dlsloan4 Reason: 1
Hello everyone,

I am trying to use a magnetic switch by sending and output from the Arduino though the switch to an input in the Arduino.

The switch is Normally Open until it contacts the magnet then the switch becomes closed.

I am using a digital input/output. When the switch is closed the serial monitor reports a 1, perfect, right?  

Yes! but, when the magnet is pulled from the switch the input continues to read a 1! this is no good.

We have also tried this with a momentary pushbutton switch, same result.

Any suggestions of why this is getting stuck?  A continuity check confirms that the switches are working properly.

I am using pin 12/13 IN/OUT.   Could using the pins right next to each other cause an issue?  or is it somethign with my code?

Code: [Select]
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
 // put your setup code here, to run once:

Serial.begin(9600); // opens serial port, sets data rate to 9600 bps

//MAG Switch Ports  
pinMode(12, INPUT);
pinMode(8, INPUT);
pinMode(13, OUTPUT);
pinMode(7, OUTPUT);
//Outputs 5v to magnetic switch
digitalWrite(13, HIGH);
digitalWrite(7, HIGH);

//Receiver Inputs
pinMode(6, INPUT);

//Motor Outputs


void loop() {
//Checks PWM for ch1-ch2
double ch1 = pulseIn(5, HIGH);
double ch2 = pulseIn(6, HIGH);

int Mag_rHs = digitalRead(8);
int Mag_lHs = digitalRead(12);

//Maps Inputs about 0


//Constrains the Receiver input
if (Mag_rHs == 1){
  ch1 = constrain(ch1,0,90);//Can only turn left and Neutral


if (Mag_lHs == 1){
  ch1 = constrain(ch1,90, 180);//Can only turn right and Neutral

if (ch1<95 && ch1 >85){

//Output to Victor Speed Controller

//Prints PWM to Serial Monitor


If the only thing connected to the input pin is one side of a switch, when the switch is open the input pin is left floating.

Usually you use a pull-down resistor from the pin to Ground and use the switch to connect the pin to +5 (open=LOW, closed=HIGH) OR you use a pull-up resistor from the pin to +5 and use the switch to connect the pin to Ground (open=HIGH, closed=LOW).
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e


So what your saying is to put a resistor at the input pin to ground? 

Sorry we are a bunch of MEs working on controls.  What value is the pulldown resistor?  Something to drop the 5v to zero to ground?  Or is it a standard value?


See this tutorial: http://www.arduino.cc/en/Tutorial/Pushbutton
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e


See this tutorial: http://www.arduino.cc/en/Tutorial/Pushbutton

This is useful too....
Gone for good

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131