Uncategorized

Driving relay with gpio pins s

Problems with the rpi zero and relay [closed]

Ask QuestionAsked yesterdayActive yesterdayViewed 36 times-1Closed. This question is off-topic. It is not currently accepting answers.


This question does not appear to be specific to the Raspberry Pi within the scope defined in the help center.

Closed 13 hours ago by joanSeamusDougieMilliwaysDmitry Grigoryev.

(Viewable by the post author and users with the close/reopen votes privilege)Edit question

I Have a problem…

(above the relay-coil Datasheet)

I want to control a single relay with my pi zero, but it seems that it doesn’t have enough power. the 3.3V pin of the rpi (measured to 3.19V) works just fine but the gpio pins (ca. 3.12V) don’t have enough voltage. Is there an easy way how to control the relay with converting the power or something like that?pi-zero-wrelaypower-managementgpiozeroshareedit  follow  reopenflagedited yesterdayasked yesterdayNilusink1744 bronze badges New contributor

  • 1What is the driving rating of your relay? – Sohan Arafat yesterday
  • 1@SohanArafat added a picture of the datasheet … – Nilusink yesterday
  • 3NEVER attempt to directly switch a relay with a GPIO. NEVER attempt to directly drive any inductive load. You will destroy the GPIO and Pi. ALWAYS use a relay module or a motor driver module. – joan yesterday
  • 1Did you happen to notice that 120 mA are required to drive the coil at 3 VDC? How much current do you imagine the RPi Zero is capable of supplying? – Seamus yesterday
  • 1It says that it would be limited to 50mA, but thats a little bit strange cause it still works with 3.3… – Nilusink yesterday
  • 1What says 50 mA? Where are you reading that? – Seamus yesterday
  • 1It stands on raspberrypi.org – Nilusink yesterday
  • 1Show me where – give me a link, please. – Seamus yesterday
  • 1raspberrypi.org/forums/viewtopic.php?t=12498 – Nilusink yesterday
  • 1They probably only mean the gpio pins witv that… – Nilusink yesterday
  • 1If you’ll read that answer carefully, he did not say a single GPIO pin can supply 50 mA. And it’s a poor answer because it doesn’t address a direct question (which is the reason SE is often a better source for information than the forum). You will be far better informed if you find multiple sources that corroborate each other. Also, look for published specifications instead of blogs and rambling discussions. Read this – Seamus yesterday 
  • 1Does this answer your question? GPIO pin voltage is too low to energize relay – Dmitry Grigoryev 13 hours ago

add a comment

2 Answers

ActiveOldestVotes3

Driving the coil of a relay requires power. Power (P) – in direct-current electrical form – may be calculated as the product of voltage (V) and current (I):

P = V * I

Knowing even a little bit about the GPIO specifications, and then looking at the numbers in your table, it is clear that the RPi Zero is simply incapable of supplying sufficient power to operate any of the relays in the spec sheet you provided. Even if the limit were 50 mA (as wrongly implied in the non-answer you referenced), the calculation shows you fall well short of what’s needed to drive your load:

Prequired = 0.36 watts (from your relay specs)

Pavailable = 3.3V * .050A = 0.16 watts (a deliberately inflated figure to make this point)

If you’ll take advantage of this Q&A exchange, you will now be able to perform some calculations on your own. After perusing some of the references for the RPi’s GPIO voltage and current limits from the link above (or this one), you may calculate how much power a GPIO is able to deliver to the coil of your relay. Just multiply the GPIO pin’s maximum current by its maximum voltage.

Once you do that, I’m confident you’ll reach the correct conclusion:

You cannot supply “abt 0.36 watts” from a GPIO pin.

Having reached that firm conclusion, and knowing that relays are in fact controlled by RPi’s, a bit more research will suggest that other external component(s) are required. The Internet, and this SE, have huge reservoirs of suggestions, schematics with part numbers, explanations, etc – all for the modest price of a search. A typical example is shown in the schematic below:

schematic

simulate this circuit – Schematic created using CircuitLab

If I were you, I’d be asking, “How much power can this arrangement deliver, Mr. Know-It-All? It’s easy enough to calculate, but we’ll need a spec sheet for the 2N2222 transistor. Here are the relevant parameters:

  • a 2N2222 transistor is biased in saturation mode when used as a switch
  • in saturation mode, with a base current of Ib= 3.3V / 330Ω = 10mA, the collector current (Ic) and the collector-emitter voltage drop (VC-E,sat) may be estimated from a spec sheet for the 2N2222 transistor:
    • Ic ~ 100 mA (conservative estimate)
    • VC-E,sat ~ 0.3 V

And so we can estimate the power delivered to the load (relay coil) with the transistor arrangement as follows:

Pavailable ≥ (5V – 0.3V) * .10A = 0.47 watts (a minimum)

Once you have some ideas, you may wish to “breadboard” them – to experiment, and learn if they work, and how well they work vs other solutions. We’re always happy to answer specific questions when the OP has done some research beforehand. We may be somewhat less receptive if it’s clear that’s not the case.shareedit  follow  flagedited yesterdayanswered yesterdaySeamus7,07011 gold badge99 silver badges2626 bronze badges

  • Your R1 seems too small. 2N2222 has a minimum current gain of 50, which means 10mA of base current is enough to produce a collector current of 0.5A. The 5V version of the relay needs only 0.089A. – Dmitry Grigoryev 13 hours ago
  • @DmitryGrigoryev: That’s true – it would very likely work fine with a larger base resistance for this particular application. – Seamus 6 hours ago

add a comment-1

This is a common issue (3.3v pin cannot reliable control a relay). The Pi does have a couple of 5v pins — but those are always on (basically a pass-through of the 5v power supply).

Typically a circuit is needed where you use the 3.3v pin to control the flow of the otherwise always-on 5v power. The 5v power throws the relay.

That’s the basic idea but there are nuances in the circuit to make it reliable and some resistors, transistors, and diodes will be involved.

It turns out you can get a “Relay HAT” (there are many of them on the market) designed specifically for the Raspberry Pi that already do all of this. If you just need to control a few relays, this is the easier way to do this. Check the specs of the boards to make sure they can adequately control the power needed for your project.

If this is meant to be a learning exercise (you want to build your own circuit rather than use a pre-built board) then there are articles that walk through the steps of what is typically needed and why. For example, here is an article that walks through how to build your own circuit (not using a pre-made relay board or HAT): https://makecademy.com/control-a-relay-from-anywhere-using-the-raspberry-pishareedit  follow  flag answered yesterdayTim Campbell42322 silver badges1111 bronze badgesadd a comment

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.