With suitable protection and not a very big solenoid(*). IIRC the GPIO pins don't have very much power available. Think it would be better for the OP to use a GPIO to drive a switching transistor to drive the solenoid at the desired 5 V.
(*) "Solenoid" to me is a electromagnetic device that produces significant mechanical movement to operate a lock or push/pull something possibly with a fair bit of force.
The rPi is a 3.3 volt device and can't drive 5 volts, you will need an interface adapter.
In particular solenoids are a bit more complex to drive than a resistive load. When you drive them they build up a magnetic field which holds energy. When you stop driving them that energy has to go somewhere and as the magnetic field collapses it generates a voltage across the coil. This voltage can damage the driver if the energy is not dissipated. So it is common to put a diode across the solenoid to safely conduct the current and prevent the voltage from building up and doing damage.
You need some sort of buffer between the Pi and the solenoid - and what this is will really depend on the voltage and current the solenoid needs. If you're just sending a 5v signal to something that then powers the solenoid, then you might find it works OK with 3.3v anyway, however if you are driving the solenoid directly, then you'll need something like a 2n2222 transistor, base current limiting resistor and a diode.
Or 1/7th of a uln2003.
Do you have a better description of what you're driving? Photos, manuals, etc.? Then we'll be in a better position to offer help & advice...
At the very least, you need a reasonably beefy darlington transistor (with series base resistor) or a power FET. Preferably ones with built-in diode protection. Even quite small solenoids can generate massive back-emf if not properly constrained. Commonly this is done with a diode, but that has the effect of slowing the release movement, so if that's important then things can get quite complex.
P.S. My credentials: I had to design a driver for a high speed magnetic clutch/brake system for a cardboard stitching machine - nightmare!
With all do respect... im fuckig in love with you guys.. i really didnt expect to receive that much information... But i think ill go what dave saidni think.. My environment : My software is a night club manager with prepaid cards that will communicate with a jukebox..
4 gpio in .. one for each music button.. 3v
4 gpio out to activate the cd.. 5v required. .. I said solenoid because was the first thing more than 3v. That I couldbremcould remember. . I just neet my multimeter gets a 5v out.. and now I think with a transistor I could get it
My apologies - what I wrote was not a clear way to express what I was thinking!
When I wrote "directly", I meant not going via some further logical process. Certainly it needs some sort of power amplification, such as a transistor or two - depending on the power requirement of the solenoid. Unless you are dealing with a micro-miniature solenoid!
But transistors can easily be driven from 3.3V outputs. Bipolar transistors are very easy. If using a FET you have to study the data sheet carefully, and get one whose channel will be adequately turned on by a gate voltage of 3.3V. Lots of FETs want rather more than that.
I would buffer the inputs with something substantial too. The inputs to the rPi go straight to the CPU which is not very tolerant of any excess voltage. In the real world there are spikes and noise that creep into designs. I would also add substantial debouncing to the software. Buttons will make and break contact for 10s and even 100s of milliseconds giving you lots of button presses. Plus you need to ignore any short pulses which may be a bit of noise.
Are there any recommended opto-isolators that would meet the OP's requirement?
I'm asking because I'm curious and also because I haven't noticed them mentioned explicitly as being suitable for use with an RPi. I assume that something like that might be the way to go for inputs, but would they be a good idea on outputs as well? If so, are there any that would produce a
5v output with decent current capability, i.e. that let you drive the two sides at different voltages?
martin@ | Martin Gregorie
gregorie. | Essex, UK
Very good advice. However, 100s of milliseconds is most likely an overestimate. For example, this switch has a bounce time of
One viable debouncing strategy is to sample the inputs at an interval greater than the rated bounce time. For a transition from state A to state B, initially you'll be sampling state A, and finally, you'll be sampling state B. If your sampling interval is greater than the switch's bounce time, you'll never sample more than once within the bounce window. Whether you sample ...A-A-A-B-B... or ...A-A-B-B-B... (the middle sample being inside the bounce window) it won't matter.
Metastability, caused by sampling an analog voltage level that is between a logic 0 and logic 1, could be another concern. In practice, if you read the value into a register, let it sit for a few clock cycles, and then use it, the metastability will have resolved itself to an expected failure rate of once during the lifetime of the device.
Optos are useful if you need isolation, meaning the input or output has to float relative to the other signals and/or the CPU. It's not really needed for driving or receiving a signal by itself. It is normally used for safety reasons or when there are high ground differences which would damage a circuit. I'm not sure the OP needs anything on the input side if, for example, the buttons are in the same box with the rPi.
I don't think metastability is an issue when reading it into a CPU input. There will be many opportunities for a metastable input to settle before it is used in a way that would cause a problem. This is a much bigger problem when used in a device like an FPGA where you might be running the input to multiple locations after the first FF.
As to the bounce time, I have been bitten a number of times by switches bouncing badly. I had one device I built that I had to add an RS-232 input because I could never get the switches to debounce reliably. I think an appropriately sized capacitor across the switch is another good idea as it greatly reduces the opportunity for such problems.
When putting a capacitor across the switch, you'd also want to consider the resistors that are in the circuit. Whether or not there is series resistance to slow down both transitions, a Schmitt trigger can also be useful. Google finds a schematic for one made from two transistors and some resistors and another using an op-amp. There are IC Schmitt triggers as well. You can also make one from a non-inverting CMOS buffer (4050, IIRC) and a couple of resistors.
Agreed that metastability is not a significant concern, when feeding a GPIO pin of a CPU.
I've used optos on a Pi - in a situation where the device I was talking to was electrically separate from the Pi (ie. no common 0v supply). they work well, you just need to treat them like any other LED and work out the limiting resistor, bearing in-mind the 8mA limit on a Pis GPIO pin.
Gordon  It's 8mA to guarantee the output stays at a level that would register as a logic 1 to a 3.3v device - you can pull 15mA from a pin but you need to change the drive strength to guarantee the output voltage stays high enough to register as a '1' to a remote device. You can pull 15mA without doing anything, but the output voltage may drop.
Noted: I see that many optos would like a bit more current than GPIO can provide and might not be altogether happy with 3.3v either. I suppose that makes the LM358 OpAmp a useful first step in interfacing outputs to TTL levels, plus they are fairly cheap and dead easy to use.
martin@ | Martin Gregorie
gregorie. | Essex, UK