Morning all.
I am trying to make an arduino touch switch using a mosfet. I have managed to get an LED to turn on when the touch pad is touched and it works very well but when i try to replace the led for an arduino pin pulled high(internally) i run into problems. sometimes the switch is read as low when i touch the pad but other times it just becomes unresponsive. could anyone see where i am going wrong? many thanks. Dan.
(ive just realised that i have mixed my letters up on the mosfet, the drain is in fact connected to arduino A0 and the sink to ground)
First of all a pullup resistor is needed, from A0 to Vcc. Then the FET will catch some ambient noise (electrical field) from mains, some wallwart(s), an ongoing WLAN, BT or RC communication.
You can pull up the gate voltage, to come near the threshold voltage of the FET (switching point), and attach a wire to increase the sensitivity of that AC receiver. But it's unpredictable what the circuit receives out of thin air, and whether and how it may react on your presence or absence.
thanks Doc,
I did manage to get it working with the internal pullup in the end. as you say it is reacting from the ambient fields around me so i suppose it wouldn't work in the countryside away from electric fields?
(i added a resistor in series with the gate to reduce likelihood of damaging it.
#define input_pin A0
#define led_pin 3
int input_data;
unsigned long timer;
bool setTimer;
bool touched = false;
bool first = true;
bool toggle = true;
void setup() {
pinMode(input_pin, INPUT_PULLUP);
pinMode(led_pin, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(led_pin, toggle);
if (digitalRead(input_pin) == LOW) {
touched = true; //set the variable on the first low which is read
setTimer = true;
}
if (touched == true && first == true) { //only allow it to happen once
first = false;
toggle = !toggle;
Serial.println(toggle);
}
if (setTimer == true) {
timer = millis();
setTimer = false;
}
if (millis() - timer > 100) {
first = true;
touched = false;
}
}
If you want a resistive touch switch, you should provide two contacts which have to be covered (shorted) by your finger. The same for capacitive switches, only with different code.
If you want a resistive touch switch, you should provide two contacts which have to be covered (shorted) by your finger. The same for capacitive switches, only with different code.
why so? i knew this was the case when using a BJT but the mosfet only needs a single contact, as i said the setup i have at the moment is working very well.
gonadgranny:
I did manage to get it working with the internal pullup in the end. as you say it is reacting from the ambient fields around me so i suppose it wouldn't work in the countryside away from electric fields?
More likely the opposite: much less electrical noise should make for a cleaner, easier to detect signal.
Do make sure you have very good grounding, as capacitive sensing stands and falls with it (which is why such projects often work fine until they're unplugged from the computer).
Do make sure you have very good grounding, as capacitive sensing stands and falls with it (which is why such projects often work fine until they're unplugged from the computer).
yes i just tried it with a battery and it no longer works. is there any way around that? thanks.