I am attempting to create an electromagnetic paper airplane launcher. I am using a hall effect sensor to detect when a sled is passing a solenoid. I am using an Arduino Uno as my microcontroller so that once the hall effect sensor detects a magnetic field, an output voltage would be supplied to the gate pin on a logic-level n channel MOSFET.
As a safety feature, I have added a push-button. In my Arduino code, I am only allowing for the MOSFET to receive an input voltage once the button is being pressed and the hall effect sensor reads a magnetic field.
I tested everything today and found that current will be supplied to the solenoids as intended, but it will also be supplied when a magnetic field is present without having the button pressed.
I am not sure if this is a coding problem or not. Before hooking the solenoid to the mosfet, I used an led in place of it in order to see if anything was being sent through the MOSFET. When I used the LED, it worked as intended--only turning on when a magnetic field was present and the button was pressed down.
I was just wondering if anyone knew what might be happening when I changed the LED with a copper wire solenoid.
Here is my code:
int coil=1;//solenoid 1
int hs=2;//1st hall effect sensor
int button=3;
int coil2=4;//solenoid 2
int hs2=5;//2nd hall effect sensor
void setup() {
//establishes the hall effect sensor pins as input, the button pin as input, and the solenoid pin as output
pinMode(hs, INPUT);
pinMode(coil, OUTPUT);
pinMode(hs2, INPUT);
pinMode(coil2, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
if ((digitalRead(button)==HIGH)&&(digitalRead(hs)==LOW))//makes sure that the mosfet will only send current to solenoid once the button has been
{ //been pressed and the hall effect sensor has been triggered.
digitalWrite(coil, HIGH);
}
else{
digitalWrite(coil, LOW);
}
if(digitalRead(hs2)==LOW)
{
digitalWrite(coil2,HIGH);
}
else{
digitalWrite(coil2, LOW);
}
}