a.) There's a sensor which generally stays high (12v) but drops to 0v when a coin is dispensed, which I'd like to read the input from. (described in s11.1 of that manual).
b.) There's also an signal line which I'd like to be able to send a 12v pulse to (described in s8.2.3 of that manual).
Essentially, I simply want the equivalent of digitalRead (a) and digitalWrite (b), but for 12v logic, and I'd be grateful for suggestions as to what the best way to do that is?
I'm guessing not relays because of their slow switching time.
For (a), I've used MOSFETs in the past to drive higher voltage loads from an Arduino 5v input, but never seen them used the other way around (to send a 5v signal to Arduino when 12v signal is received at the gate?).
For (b), I tried a IRF520 MOSFET but had some problems - while I could apply +12v to the line by applying 5v from digitalWrite(HIGH) at the gate, when I set digitalWrite(LOW) the pulse didn't drop to 0v but become free-floating. I'm guessing this is either because of poor MOSFET choice (I don't have any logic-level MOSFETs handy), or because I need a pull-down resistor at some point, but am getting myself in a bit of a pickle to working out where.
Any suggestions or help gratefully received. Thanks!
You could use an optoisolator in both cases: it's just a transistor with open collector, the base's led activated by the signal.
For your (a), where you want to "digitalRead 12V", choose a series resistor for the opto's led that will be ok for a 12V input, and just take the opto collector to an Arduino input with pullup turned on. Your logic will be reversed, since the presence of 12V will close transistor and take the pin low, but handle that in your code.
For (b) where you want to "digitalWrite 12V" take your 5V output to the opto's led with a series resistor, and connect the opto's collector to 12V with a pullup. Then 5V on the base will close the transistor, and drop the 12V to 0V. (Also handle the backwards logic in code.)
Thankyou for the swift response. Yes, that sounds like it would solve the problem - I'm not that familiar with opto-isolators, but from what I've just read that means I can ensure the 12v/0v pulse is relative to the isolated "ground" of the logic circuit, rather than the shared ground of a MOSFET (which I think might be the cause of my current problem).
From a quick look round, it seems that the Sharp PC817 is cheap and readily available, so I wondered if you could just quickly check that I've understood correctly in this diagram/description?
The top half is concerned with sending a 12v pulse TO the logic circuit:
When digitalWrite(LOW) is sent to Pin12 (yellow wire), the optocoupler is detached and the 10kΩ pull-up resistor at B causes the Logic "IN" Control on the right hand side to read HIGH.
When digitalWrite(HIGH) is sent to Pin12, the LED turns on (with protection from the Arduino output pin provided by the 220Ω resistor A), and causes the Logic "IN" to read LOW instead.
The bottom half is concerned with reading 12v signal FROM the logic circuit:
I've seen different datasheets for PC817s with different forward Voltages/currents, but I've put a 470Ω there at C just to remind myself it needs to drop the 12v supply for the optoLED on that side - when I get hold of an actual PC817 I'll check the exact specs.
I haven't put a pullup on the other side as I'll just initialise Pin8 with INPUT_PULLUP and then digitalRead from that. When the logic OUT sensor is LOW, this will read HIGH, and when the logic circuit is HIGH, the optocoupler will engage and this will read LOW.
Have I understood correctly? Thanks for your help!
Your resistor at B isn't a pullup to 12V. You need to put 12V on there all the time, and move the brown wire to the bottom of that resistor, ie read the opto's collector. With 12V on the top end of the resistor, the collector will be 12V with the opto open, and pulled to ground through the emitter if the opto is closed.
(That might be a mistake you've already made with your mosfet circuit. It can't get 12V out of thin air: it needs a 12V supply from somewhere.)
Bottom part looks right except your orange wire's not connected to the opto at all
Also your grounds on the left are on two different, unconnected, rails.
I'm guessing those last two points are just mistakes in the drawing?
If you need isolation, above is appropriate method to do it.
If you do not, I would:
Tie grounds together.
For receiving the LOW pulse, diode with band towards the coin acceptor, pin set INPUT_PULLUP.
For sending the pulse (note that the pulse is LOW not HIGH! The line is +12v while idle as I read that "A pulse is defined as a falling edge followed by a rising edge" ), I would pull the line up to +12v with a resistor (if there isn't already a pullup on that thing), and connect it to the drain of a small MOSFET - source to ground, and gate through a 100 ohm resistor to the arduino output pin.