My project: I have a Citroen ami microcar without retro light and I would like to add one.
I have to intercept the button press on the gear control REAR to switch on the light and the button press to NEUTRAL or DRIVE to switch the light off
What I have till now: I managed to write the code, and tested with the simulator with 2 simple switches and a led.
#include <ezButton.h>
const int buttonNPin = 2; // Connect button N to Arduino pin 2
const int buttonRPin = 3; // Connect button R to Arduino pin 3
const int ledPin = 13; // Connect the LED to Arduino pin 13
bool ledState = LOW; // LED state (LOW or HIGH)
ezButton buttonN(buttonNPin);
ezButton buttonR(buttonRPin);
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
buttonN.setDebounceTime(50);
buttonR.setDebounceTime(50);
}
void loop() {
buttonN.loop();
buttonR.loop();
if (buttonN.isPressed()) {
ledState = LOW; // Turn OFF the LED
}
if (buttonR.isPressed()) {
ledState = HIGH; // Turn ON the LED
}
digitalWrite(ledPin, ledState);
}
What I am missing?
I can't understand how to hook up this circuit to a momentary switch connected to a 12v battery car. I read about using NPN transistor but I am trying to setup in the simulator without success.
Can someone give me some hints?
What kind of Arduino do you suggest?
You connect the led (or light bulb) anode to +12v. You connect the led cathode to the collector of an NPN transistor. You connect the emitter of the NPN transistor to ground. Applying power (via a current limiting resistor say 680R) will switch the light on.
Search for "using a transistor as a switch" for more information.
Thank you for answering me . Maybe I am missing something basic, excuse me but electronics isnt' my field
The buttons are the one mounted on the car , Rear and Neutral they switch a 12 volts tension
I need to intercept the contact of this switch and send it to the arduino for switch the led on .
I don't have to feed a power device trough Arduino, I have to use a to power signal in a digital input of Arduino.
Hope I managed to explain me better, maybe I could use an optocoupler?
To supply a signal from a 12 volt circuit to a 5 volt arduino you must use a voltage divider (consisting of 2 resistors) to reduce the voltage from 12 to 5 volts. You'll get the idea from here: Voltage Divider Calculator. You could alternatively use an optocoupler as you have suggested. In that case, you have to use a current limiting resistor to reduce the current flowing through the opto coupler led to something like maximum 20mA. The first schematic in this link [Noob question] Can you use an optocoupler to act as a button for input/output? - #2 by jremington should give an idea (the resistor should be higher in your case though (as 1k).
Thank you very much for taking the time to answer me. The voltage battery of the car can range between about 14 and 12.5 volts if I consider 14 volts calculating the voltage divider will be the Arduino input tolerant with these variations.