Go Down

Topic: Magnetic Switch or Push button Switch, getting stuck? (Read 2125 times) 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: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


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: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


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

This is useful too....
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

Go Up