By continuing to use this site, you agree to our use of cookies. Find out more
Forum sponsored by:
Forum sponsored by Forum House Ad Zone

Difficulty Operating Solenoids with an Arduino Uno

All Topics | Latest Posts

Search for:  in Thread Title in  
James Alford28/12/2020 23:02:11
501 forum posts
88 photos

I wonder whether anyone can help?

I wish to operate some 6 - 12v solenoids with an Arduino Uno. I have written the code and built a circuit Circuitwhich uses transistors and an additional power supply to avoid overloading the Uno.

The Uno is powered through the power jack using a transformer. The additional power is supplied by a 9v battery which has its negative lead connected to the GND pin on the Uno. I have wired up the circuit in the attached diagram. The LED shown in the circuit is there simply to show that the circuit is working.

Everything works as expected except that the transistor is passing only 1.5v to the output terminals shown as A and B in the diagram. However, I was anticipating in the order of 9v. Either I have misunderstood what this circuit is meant to do or I have made an error in the design.

Can anyone advise what I have done wrong, please, and how I can get 9v at the output?

Regards,

James.

duncan webster28/12/2020 23:16:54
5307 forum posts
83 photos

Your transistor has the load between 9v and collector. There should be 9v at terminal A at all times With the solenoid connected between A&B and pin 13 high, the transistor should conduct and the voltage at B should be very low. With pin 13 low, the transistor will not conduct and so the voltage at B will be 9v.

I reckon you'e got the LED wrong way round

Stuart Smith 528/12/2020 23:29:04
349 forum posts
61 photos

Edited By Stuart Smith 5 on 28/12/2020 23:31:32

Edited By Stuart Smith 5 on 28/12/2020 23:31:55

Edited By Stuart Smith 5 on 28/12/2020 23:35:39

AJAX28/12/2020 23:49:19
433 forum posts
42 photos
Posted by Stuart Smith 5 on 28/12/2020 23:29:04:

You have some of the connections wrong. This should work:

1eb69bc8-e1a5-47f3-a216-19bd37ae04de.jpeg

With Arduino pin 13 output high, the solenoid coil should be energised.

I think!

Stuart

Edited By Stuart Smith 5 on 28/12/2020 23:31:32

Edited By Stuart Smith 5 on 28/12/2020 23:31:55

I believe that James wants the LED to act as an indicator for the solenoid (LED on when the coil is energised). The LED therefore needs to be placed in parallel (forward biased) with the solenoid load.

The solenoid is an inductive load and the circuit should be protected with a freewheeling or flyback diode. 1N4001 in reverse bias with the solenoid would be fine.

The use of low-side switching where the load is placed on the collector side of the the transistor "switch" is correct for this application.

Assuming pin 13 outputs a logic high of about 5V and Vbe 0.7V this will draw about 20 mA base current. BC547 is a small signal transistor and not ideally suited to switching solenoids and you'd have to be careful of the expected collector current.

For a switching application I would suggest use of a logic-level N-channel MOSFET. Off the top of my head, an IRF3205 would be fine and would be able to handle substantially more current than you will switch. The MOSFET requires no resistor on the gate. Any charge on the gate due to gate capacitance would discharge (sink) back to pin 13 during logic low.

Do you need a circuit drawn?

Brian

AJAX28/12/2020 23:57:02
433 forum posts
42 photos

Another point, most standard 5mm red LEDs have a maximum forward current of about 15 - 20 mA.

Check the data sheet if you have one.

If not, choose a current limiting resistor that limits current to about 10 mA. You can assume Vf is about 2V.

AJAX29/12/2020 00:13:27
433 forum posts
42 photos

solenoid.jpg

SillyOldDuffer29/12/2020 09:52:57
10668 forum posts
2415 photos

The switching part of James' circuit looks OK to me, but a few things to try:

  • C and E may be the wrong way round on the BC547B? According to the datasheet, the transistor lead order is C B E, not E B C.
  • Confirm the source code in setup() makes the pin an Output with pinmode( 13, OUTPUT ); If the pin isn't declared OUTPUT, it signals with insufficient oomph to switch a transistor.
  • Use any other pin rather than 13! Pin 13 powers the Arduino's on board LED, which might matter in this application because the built-in LED absorbs current otherwise needed for switching the BC547B. (It should work as is, but...) AJAX's circuit avoids this problem with an IRF3205 FET. These are switched by voltage, requiring hardly any power, but NPN transistors are switched by current so you have to keep an eye on pin and chip maximum ratings.
  • Don't measure from A to B. Instead connect the solenoid or a 1k test resistor and measure volts from B to battery minus. B should float at 9V until the transistor is switched on, at which point volts at B should drop to near zero. Provided the solenoid is a 9V type drawing less than 100mA, it should work.

Dave

James Alford29/12/2020 11:17:34
501 forum posts
88 photos

Good morning and thank you for the replies and advice.

I have had a look and a play with the circuit, testing it as suggested by Dave. Everything operates as I want it to do, with the solenoid pulsing each second. The circuit is for a clock and the other pins are programmed for the minute, hour, day and so forth. These all trigger the solenoid as planned.

My puzzlement was that I could only read 1.5v across A and B with no load attached. Perhaps, most likely, the problem is my weak knowledge of electronics, rather than an actual problem.

As a round-up of the current position:

  • Yes. I am using the LED to flash at the same time that the relay operates as a visual indicator.
  • The relay is operating as expected on all programmed pins, including 13.
  • I removed the LED to see what would happen and nothing changed.
  • Testing as Dave suggests shows 9v until the transistor switches, when the voltage drops by about 1.5v. However, the "on" pulse is very short, so it may be that my AVO cannot respond quickly enough to show the true fall in voltage.
  • The solenoid in use is meant to draw 0.25a, but I am measuring it at 25ma.

My questions now are:

  • Is my cicruit likely to overload the Uno? The are six relays in total, but they operate at marginally different times.
  • Is there a need to change the transistor, given the current and the voltage in question? If so, is there one which I could use as a direct replacement in the existing curcuit? I have six in total.

Thank you.

James.

Would changing the transistor

Bazyle29/12/2020 11:36:06
avatar
6956 forum posts
229 photos

I haven't studied the above in detail but bear in mind with similar small controllers that they are often not designed to source much current from their output pins so while they can often sink currnet to drive a LED they can't provide much current to turn on a transistor. Check the specs.

Also note the way Ajax has drawn the diagram. There is a convention in electronics for the way you draw things that makes it far far easier to understand. I have encountered unfortunate HNC students totally failing because their idiot teachers did not follow the conventions so their pupils were at a massive disadvantage. When shown the conventional layout comprehension increased tenfold.

John Haine29/12/2020 11:56:16
5563 forum posts
322 photos

I have an Arduino Nano sourcing 15 mA driving a small coil on the clock on my desk, and about 10 mA to drive the movement so there should be no drive current issue into the base of the transistor.

As Bazyle says the way the circuit is drawn is rather bizarre which makes it hard to read and comment.

We can't comment on the transistor choice without knowing more about the solenoids, in particular their resistance. But mosfets as suggested above would be the preferred choice these days as they don't need any current to drive them (to speak of). Ah, you said it's meant to draw 250 mA, but that would be the static current - how are you measuring your 25 mA, with an AVO on the pulse? See comment below.

Are you using an "analogue" AVO and how long is the pulse? Why not just make it longer for test purposes? Whether a "real" AVO or a digital variant, they have a time constant of half a second or so so that would easily explain the reduced reading

SillyOldDuffer29/12/2020 12:24:38
10668 forum posts
2415 photos
Posted by James Alford on 29/12/2020 11:17:34:
  • Testing as Dave suggests shows 9v until the transistor switches, when the voltage drops by about 1.5v. However, the "on" pulse is very short, so it may be that my AVO cannot respond quickly enough to show the true fall in voltage.
  • The solenoid in use is meant to draw 0.25a, but I am measuring it at 25ma.

My questions now are:

  • Is my cicruit likely to overload the Uno? The are six relays in total, but they operate at marginally different times.
  • Is there a need to change the transistor, given the current and the voltage in question? If so, is there one which I could use as a direct replacement in the existing curcuit? I have six in total.
...

The maximum output from an individual Uno pin is 40mA (but never exceed 30mA because they go pop!), and the total current taken by all output pins must be less than 100mA. With a 220ohm resistor between pin 13 and the transistor base, the max current from I=V/R will be 23mA, or 128mA in total if all six relays are operated at the same time. No problem doing 4 relays together, but all 6 is pushing it. Increasing the 220 ohm resistor to 330ohm would add a safety margin.

AJAX mentioned the BC547B being a small signal transistor (max 100mA) isn't ideal for a solenoid, and it's certainly too small for a 0.25A device! I'd substitute a switching transistor like the 2N2222A, which is good for 0.8A. Same circuit, just a beefier transistor. (AJAX's FET is good for over 100A, and needs less drive current!)

The pulse length may need to be increased. Solenoids take time to react, and for the same reason an Avo needle is too slow to follow a fast pulse. Best that can be said if your pulse is less than, say 0.5s, is that the current drawn by the solenoid is at least 25mA, probably much more. Not a criticism of Avo's - most basic meters take a few seconds to settle, which is why an oscilloscope is so useful.

The A-B puzzlement is natural enough! It's because pulling B down to ground with the transistor causes current to to flow whereas the meter measures volts. Measuring B to ground allows a voltmeter to clearly register the circuit is working because it swings from 9V to Zero. Volts from A to B depends on load resistance so you get non-obvious readings.

Dave

Andrew Johnston29/12/2020 12:31:48
avatar
7061 forum posts
719 photos

Posted by AJAX on 28/12/2020 23:49:19:

For a switching application I would suggest use of a logic-level N-channel MOSFET. Off the top of my head, an IRF3205 would be fine...........................

The IRF3205 doesn't look like a logic level MOSFET according to the datasheet I downloaded? A simple NPN transistor, such as the FMMT491, will work fine. For bipolars it's good practice to have a resistor from base to emitter, say 10k. Same for a MOSFET, say 100k from gate to source. When processors power up most GPIO pins come up as inputs by default. The purpose of the resistors is to ensure that the transistor is off when the base/gate is essentially floating.

I'd agree with John Haine that an Avo is not the best choice of meter; a 'scope would be ideal. It's not true to say that MOSFETs need no drive current. They can require substantial current, albeit for a short time. On the datasheet gate charge is the key parameter. For digital ICs the majority of the current consumption is due to charging and discharging the gates during switching.

Andrew

AJAX29/12/2020 12:35:37
433 forum posts
42 photos
Posted by SillyOldDuffer on 29/12/2020 09:52:57:

 

  • Confirm the source code in setup() makes the pin an Output with pinmode( 13, OUTPUT ); If the pin isn't declared OUTPUT, it signals with insufficient oomph to switch a transistor.
  • Use any other pin rather than 13! Pin 13 powers the Arduino's on board LED, which might matter in this application because the built-in LED absorbs current otherwise needed for switching the BC547B. (It should work as is, but...) AJAX's circuit avoids this problem with an IRF3205 FET. These are switched by voltage, requiring hardly any power, but NPN transistors are switched by current so you have to keep an eye on pin and chip maximum ratings.

Port bits ("pins" ) used as outputs must be declared as outputs. Outputs don't have extra "oomph". Inputs configured with internal pullups may be a cause of confusion?

Current is not "absorbed". I realise this is an attempt to explain the situation using layman's terms, but it may be useful for the unaware to read about charge conservation and Kirchoff's current law (KCL).

An ideal MOSFET ("ideal" doesn't really exist) has an infinite gate impedance and will not draw any current. A real world MOSFET such as the IRF3205 has a very high gate impedance and small gate capacitance and so any transitory current (to charge the gate) can be ignored.

In some applications it may be necessary to add a resistor between the microcontroller pin and the MOSFET gate, but in this application it is not required.

Edited By AJAX on 29/12/2020 12:36:05

AJAX29/12/2020 12:45:46
433 forum posts
42 photos

The maximum output from an individual Uno pin is 40mA (but never exceed 30mA because they go pop!), and the total current taken by all output pins must be less than 100mA. With a 220ohm resistor between pin 13 and the transistor base, the max current from I=V/R will be 23mA, or 128mA in total if all six relays are operated at the same time. No problem doing 4 relays together, but all 6 is pushing it. Increasing the 220 ohm resistor to 330ohm would add a safety margin.

AJAX mentioned the BC547B being a small signal transistor (max 100mA) isn't ideal for a solenoid, and it's certainly too small for a 0.25A device! I'd substitute a switching transistor like the 2N2222A, which is good for 0.8A. Same circuit, just a beefier transistor. (AJAX's FET is good for over 100A, and needs less drive current!)

 

Check the datasheet for the 2N2222A - https://www.farnell.com/datasheets/296640.pdf

Ic (max) = 800 mA

This is an absolute maximum rating.

The suggested MOSFET is a voltage operated device and for most purposes can be assumed to draw no current on the gate input. As you say, it is capable of switching a large current but it can also be used to switch a very small current too. RDS(on) is very low, only 8 mOhm making it good for power applications.

https://www.digchip.com/datasheets/parts/datasheet/232/IRF3205-pdf.php

Edited By AJAX on 29/12/2020 12:46:12

AJAX29/12/2020 12:55:16
433 forum posts
42 photos
Posted by Andrew Johnston on 29/12/2020 12:31:48:

Posted by AJAX on 28/12/2020 23:49:19:

For a switching application I would suggest use of a logic-level N-channel MOSFET. Off the top of my head, an IRF3205 would be fine...........................

The IRF3205 doesn't look like a logic level MOSFET according to the datasheet I downloaded? A simple NPN transistor, such as the FMMT491, will work fine. For bipolars it's good practice to have a resistor from base to emitter, say 10k. Same for a MOSFET, say 100k from gate to source. When processors power up most GPIO pins come up as inputs by default. The purpose of the resistors is to ensure that the transistor is off when the base/gate is essentially floating.

I'd agree with John Haine that an Avo is not the best choice of meter; a 'scope would be ideal. It's not true to say that MOSFETs need no drive current. They can require substantial current, albeit for a short time. On the datasheet gate charge is the key parameter. For digital ICs the majority of the current consumption is due to charging and discharging the gates during switching.

Andrew

Thanks for making me check the datasheet.

https://www.digchip.com/datasheets/parts/datasheet/232/IRF3205-pdf.php

Fig 3. Typical Transfer Characteristics indicates a drain-source current of about 50A with a 5V logic signal from the microcontroller. It may not be a true logic level MOSFET but should be fine for this application.

 VGS(th) Gate Threshold Voltage 2.0 ––– 4.0 V VDS = VGS, ID = 250µA

AJAX29/12/2020 12:59:39
433 forum posts
42 photos

IRF3205

https://www.digchip.com/datasheets/download_datasheet.php?id=436025&part-number=IRF3205

irf3205.jpg

Edited By AJAX on 29/12/2020 13:07:09

AJAX29/12/2020 13:02:42
433 forum posts
42 photos

IRL540N is a logic level MOSFET that may suit.

https://docs.rs-online.com/8d29/0900766b80028e3a.pdf

irl540n.jpg

Edited By AJAX on 29/12/2020 13:06:19

AJAX29/12/2020 13:17:38
433 forum posts
42 photos
Posted by James Alford on 29/12/2020 11:17:34:

My questions now are:

  • Is my cicruit likely to overload the Uno? The are six relays in total, but they operate at marginally different times.
  • Is there a need to change the transistor, given the current and the voltage in question? If so, is there one which I could use as a direct replacement in the existing curcuit? I have six in total.

Thank you.

James.

James,

I have only just read your updated question.

Short answer - don't use a transistor. Don't use a MOSFET. Use a Darlington array (ULN2803)

"The ULN2803A device is a 50 V, 500 mA Darlington transistor array. The device consists of eight NPN Darlington pairs that feature high-voltage outputs with common-cathode clamp diodes for switching inductive loads. The collector-current rating of each Darlington pair is 500 mA. The Darlington pairs may be connected in parallel for higher current capability. Applications include relay drivers, hammer drivers, lamp drivers, display drivers (LED and gas discharge), line drivers, and logic buffers. The ULN2803A device has a 2.7-kΩ series base resistor for each Darlington pair for operation directly with TTL or 5-V CMOS devices. "

datasheet

Note that base resistors are in the package so you don't need to add resistors in-line with the microcontroller outputs. You should make use of the internal diodes to clamp any inductive spikes from the loads.

Brian

SillyOldDuffer29/12/2020 13:54:21
10668 forum posts
2415 photos
Posted by AJAX on 29/12/2020 12:35:37:
Posted by SillyOldDuffer on 29/12/2020 09:52:57:

  • Confirm the source code in setup() makes the pin an Output with pinmode( 13, OUTPUT ); If the pin isn't declared OUTPUT, it signals with insufficient oomph to switch a transistor.
  • Use any other pin rather than 13! Pin 13 powers the Arduino's on board LED, which might matter in this application because the built-in LED absorbs current otherwise needed for switching the BC547B. (It should work as is, but...) AJAX's circuit avoids this problem with an IRF3205 FET. These are switched by voltage, requiring hardly any power, but NPN transistors are switched by current so you have to keep an eye on pin and chip maximum ratings.

Port bits ("pins" ) used as outputs must be declared as outputs. Outputs don't have extra "oomph". Inputs configured with internal pullups may be a cause of confusion?

Current is not "absorbed". I realise this is an attempt to explain the situation using layman's terms, but it may be useful for the unaware to read about charge conservation and Kirchoff's current law (KCL).

An ideal MOSFET ("ideal" doesn't really exist) has an infinite gate impedance and will not draw any current. A real world MOSFET such as the IRF3205 has a very high gate impedance and small gate capacitance and so any transitory current (to charge the gate) can be ignored.

In some applications it may be necessary to add a resistor between the microcontroller pin and the MOSFET gate, but in this application it is not required.

Someone hold my coat - it's another violent agreement!

Have you tested what happens when an Arduino pin isn't declared to be an output correctly? I mentioned it because it's a common coding mistake causing peculiar results. Try this program:

nooutputcode.jpg

Code compiles OK and an oscilloscope shows the expected waveform. But that curvy falling edge is odd:

nooutputwaveform.jpg

Recompiling the same program with pin13 rather than 12 produces no output because the LED and resistor 'absorb' the ghost. (Kirchoff is dead...)

This version with OUTPUT enabled works correctly on both pins:

withoutputcode.jpg

And the waveform now has a proper sharp falling edge.

withoutput.jpg

I think the ghost appears when the pin's output transistor isn't enabled because there's enough leakage for a high impedance scope to see volts. But there's no oomph, sorry current, behind it. The output looks OK and might drive a high-impedance load, but is highly likely to cause odd symptoms!

True a MOSFET needs no resistor on the gate, but having one does no harm either. Isn't a 2N2222A man enough for this application? The solenoid is 0.25A and the transistor is max 0.8A.

Dave

James Alford29/12/2020 14:17:28
501 forum posts
88 photos

Just quickly.

I have a genuine AVO. I do not have access to an oscilloscope, sadly. The pulse duration was chosen fairly randomly and can be extended. The will not all operate at the same time. They are programmed to operate sequentially.

Scarily, I did electronics as part of my telephony apprenticeship, but it was theoretical and yonks ago.

James.

Edited By James Alford on 29/12/2020 14:19:16

Edited By James Alford on 29/12/2020 14:21:33

All Topics | Latest Posts

Please login to post a reply.

Magazine Locator

Want the latest issue of Model Engineer or Model Engineers' Workshop? Use our magazine locator links to find your nearest stockist!

Find Model Engineer & Model Engineers' Workshop

Sign up to our Newsletter

Sign up to our newsletter and get a free digital issue.

You can unsubscribe at anytime. View our privacy policy at www.mortons.co.uk/privacy

Latest Forum Posts
Support Our Partners
cowells
Sarik
MERIDIENNE EXHIBITIONS LTD
Subscription Offer

Latest "For Sale" Ads
Latest "Wanted" Ads
Get In Touch!

Do you want to contact the Model Engineer and Model Engineers' Workshop team?

You can contact us by phone, mail or email about the magazines including becoming a contributor, submitting reader's letters or making queries about articles. You can also get in touch about this website, advertising or other general issues.

Click THIS LINK for full contact details.

For subscription issues please see THIS LINK.

Digital Back Issues

Social Media online

'Like' us on Facebook
Follow us on Facebook

Follow us on Twitter
 Twitter Logo

Pin us on Pinterest

 

Donate

donate