//This program was written by Richard Foreman firstname.lastname@example.org the key debounce routine I used was something I found at the following //website:https://www.arduino.cc/en/Tutorial/Debounce#toc5 and the web site gave the following for authorship:// created 21 Nov 2006// by David A. Mellis// modified 30 Aug 2011// by Limor Fried// modified 28 Dec 2012// by Mike Walters// modified 30 Aug 2016// by Arturo Guadalupi// The debounce routine has only minor alterations from what I found, I just made it capable of keeping track of two keys at the same time. The rest of the code is mine//I make no waranties as to the suitability or even the ability for this program to handle anything at all. you accept all risks and liabilities if you decide to use it.//just remember, if it werent for people like us the firemen wouldnt have much of a job. so live a little, do something above your skill level, and hopefully if it smokes//it wasnt expensive lol.// This is the basic vehicle program, it operates thus. The black button is for starting and stopping the vehicle, it can be run either by pressing the black key//momentarily then releasing it in which case it will automatically start or stop the vehicle (depending on if it is already running or not) or, you can hold//down the black key and press the green key, this will crank the motor for as long as your holding the green key, when you release the green key the starter should//stop cranking the motor but the motor should stay running. At this point if you have the timer enabled it will run until the run period which you set has expired.// The green button is to toggle the timer on and off and to set the runtime. To set the runtime hold down the green key and each time you press the black key it//will add ten minutes to the runtime. When finished release the green key and the program will blink the timer led once for each ten minute increment you set.//You can only set the timer when the timer is being toggled on so if it is already on youll have to toggle it off, wait a few seconds for the wait timer, then toggle it//back on, when you press it to toggle on hold the green key down while you set the timer.// The code is fully commented, but a few words, most of the variables you mnay wish to change will be found in the global area. //The CRANKTIME is the amount of time in seconds that you want to crank the motor (I plan on adding the hardware and the code for a more robust way to determine when//the cranking should end but for now this works rather well on my vehicle.//The DEADTIME is the time between turning on the power to the vehicle and when you start cranking over the motor, some vehicles need more time then others to get ready.//the WAIT is the amount of time the program waits between key presses before registering another. This could probably be done away with its just here because I had//a problem with my debounce routine that I believe is fixed now.//The SECS and MINS were because I thought the millis() counted ticks (the way they do in computers) so that was there to make the time calcs easier.//timeron is a flag for the timer, carrunning is the flag for if the vehicle is running or not, and waiting is the delay I put there for between accepting key presses.//button states and debounce times are for the debounce routine the only one you may wish to change would be the debounceDelay.//The runtime is the variable that holds whatever you set it to by the green key (the default is 30 minutes).//The startime keeps track of when the vehicle was started the last time, the stoptime keeps track os when the vehicle should be killed,and the waittime keeps track//of when the waiting period is over.//A few notes on the circuitry. I do not have any training in electronics, the little bit I know (and I dont claim to know anything at all with any confidence) I learned//by tinkering around with crap (and smoking a few circuits along the way). This current incarnation is meant to be rather simple, all its meant to do is start and stop the //vehicle. I spend a lot of time in my van in the summer time, I have the back set up with a bed and a heater (and a camping stove, sleeping bag, fishing poles, guns)//and I love my electronics so I have 2 car batteries in the back of the van (hooked in parallel) that are in addition to the one in the motor compartment. I have a relay//up front that engages when the motor is running so that the alternator will charge the batteries when its running but my electronics will not drain the motor battery when its not.//This is meant to be the starting point for a more intricate system. You see when the back batteries drain to the point that it wont keep my electronics going everything just dies//and I hate that, so in the future I plan on adding the ability to monitor the batteries and when they drain to a certain point it will start the van automatically and run until they//are charged. I also plan on adding the hardware to monitor the vehicle for overheating, low oil pressure, low fuel and have it handle all that automatically. I am also//considering having it control the heater but we will see, one step at a time.//lets start with how I currently have it wired. I have an lm358 dual op amp set up as a comparator. Pin 8 goes to +12 volts, pin 4 to ground, pins 2 and 6 (the inverting pins)//are hooked into a voltage divider with 3 10 ohm + 2 4.7 ohm resisitors in series coming off the 12 volts and a 10 ohm rersistor going to ground (the numbers came out to 39 ohm//from the 12 volt side and a 10 ohm to ground to give me 2.5 volts 250 ma at the inverting side of the comparators but those ressistors were the closest I could get)//pin 3 gets hooked up to the RTRAN pin on the arduino, pin 5 goes to the STRAN pin on the arduino, pin 1 goes to the coil on a 12 volt relay to power up the vehicle, and pin 7 goes to //coil of a second 12 volt relay to engage the starter (of course the relays the other side of the coils go to ground, ,the common pins go to whatever the relay is driving, and the//normally open pins are hooked up to +12 volts). There is also a wire coming off of pins 3 & 5 which go to LEDs for a visual cue of when the power and starter are engaged. Then there //is a lead going from the TLED pin on the arduino to an led for a visual indication if the timer is active or not, and a lead going from the +5 on the arduino to a fourth led//just to show it is powered up. I have a lead going from +5 on the arduino to both the buttons, a 1k resistor going from the other side of each button to ground, and a lead//going from the lead with the resistor on the green button to the GBUT pin on the arduino and a lead going from the lead with the resisitor on the black button going to//the BBUT pin on the arduino. Of course I have a resistor on the cathode side of each led going to ground. I have the arduino powered from the batteries in the back through//the jack on the arduino, I am pulling the +12 volts from the vin pin on the arduino to feed the dual op amp. The grounds are being fed through the arduino, and the +5 volts//are coming from the +5 volt pin on the arduino.//I will update this as I figure it out and when I start adding capabilities to it, Ill hand all that out also.
I would confidently say I know enough in electronics to get into trouble but not enough to know how to avoid those troubles.
. . .One of your replies mentioned current limiting resistors to the transistors. I do not have them anywhere around the transistors. The relays are only pulling like 250 ms or so as they are just running the coils, do I need resistors? And do IIjust need them at the base (that's only taking like 5 volts). Or do I need them at the emitter of the second transistor that is receiving the 12 volts?
One of your replies mentioned current limiting resistors to the transistors. I do not have them anywhere around the transistors. The relays are only pulling like 250 ms or so as they are just running the coils, do I need resistors? And do IIjust need them at the base (that's only taking like 5 volts). Or do I need them at the emitter of the second transistor that is receiving the 12 volts?
It has been modified and modified and there is not circuit, just a "description".It was written for a particular application.Even Lady Ada has had a go. (Limor Fried)"I do not have any training in electronics" is a bit worrying in the "circuit" description.Tom...
If it's for a public road vehicle.... then consider safety (your own, and other people) very carefully. But if it's not for public road, then ..... still consider safety carefully.
Yes, you need current limiting resistors on both of the transistor bases. You probably also need new transistors if you ran the circuit you've drawn without them.Without knowing the characteristics of the relay, it's nominal current in particular, I've drawn a circuit with resistors below. This was done in the online circuit simulator at http://www.falstad.com/circuit/circuitjs.html which you might want to take a look at.
Hi,This is how you should be controlling your relay with the minimum of current and components and an NPN transistor.
I couldn't get it to work with the single transistor, no matter what I tried I could not get the full 12V out of the transistor with the 5v signal from the arduino for some reason (the transistors I was using are the 2n3904 and 2n3906). Granted I do not know and understand a whole lot about transistors, but if Im understanding it correctly the base of a npn transistor needs to be forward biased with respect to the emitter (which I took to mean that the base needs to be at a higher potential compared to the emitter which the arduino cant do when youre trying to run a 12v source through the transistor). I thought Id outsmart it and use a pnp transistor at that point (since it works in the opposite direction I knew I could get the base to a lower potential compared to the emitter just by sending the base to ground), however I couldn't get it to work that way either.
You are trying to HIGH SIDE SWITCH so you will need a PNP and a NPN.Like this;Tom....
Nothing to do with a Darlington. It's just two switches, an inverter (npn) driving a high side switch (pnp) for the relay. Nothing more, nothing less.
For a darlington, imagine two npn transistors, where the emitter of the first feeds into the base of the second, and the collectors both go to the supply.