How to use transistors to switch modules on and off?

I searched the forum but couldn’t find anything specific to what I’m trying to do so I thought I should make a new post.

I’m building a home environment monitor using an Arduino Mega 2560 with the following sensors and modules connected:

20x4 LCD
SHT31 humidity and temperature sensor
MQ2 gas sensor
Electret microphone breakout
BMP581 pressure sensor
PMS7003 particulate matter sensor
LIS3MDL magnetometer
Photoresistor
Adafruit’s Ultimate GPS
3 push buttons connected to a SN74HC14N Schmitt trigger (for switch debouncing)

I also plan to add an ESP-01 and micro SD card reader for connectivity and data logging.

Currently the Arduino is powered by the 9v barrel jack, and the sensors are powered by a breadboard ready USB cable that outputs 2A at 5V. But even with the extra power supply, it seems like it’s not getting enough power. The LCD display dims out or doesn’t turn on sometimes, the Arduino seems to crash randomly at times, and some of the sensor reads don’t get updated as they should.

I’m only reading one sensor at a time, so I thought I could save power by switching each module on only when it is being used, and turn it off when it’s not being used. I read some posts that do something similar using a P channel MOSFET transistor connected to each sensor’s Vcc pin. But they didn’t mention which specific P channel MOSFETs would be suitable, and how to connect them.

After looking for P channel MOSFETs online, it looks like most of them are in the TO-220 package. Are there any P channel MOSFETs in the typical TO-92 transistor package that anyone knows? That would be more suitable as I’m trying to put as many components as possible on a prototyping shield and the TO-220 package seems a bit large for that.

Also, how should the transistors be connected? I will be putting the Arduino to sleep for one minute intervals (in data logging mode), so all of the modules would need to be powered off during that time. So the transistor would need to switch off with a 0V signal, and turn on with a 5V signal once the Arduino wakes up, right? Does anyone have a schematic for this type of setup?

I know how to write the code to do this, I’m just struggling to find the right hardware to do the switching. If anyone has any ideas or suggestions it would be greatly appreciated!

What are you feeding into the barrel jack? The Arduino isn't a power supply. Have you calculated your max draw? Size the ps 25% lareger. Get the power issue solved before fixing code

Wiring problems. Please post a pic of a hand drawn (not Fritzing) wiring diagram, with all pins, connections and parts clearly labeled.

Do an image search for "arduino transistor switch" for a variety of possibilities.

Handy summary by a forum member:

1 Like

Simple sensors like a thermistor can be switched as LEDs. But sensors that are initialized with parameters from the uC cannot be powered-off without resetting the uC. Some sensors have internal uCs that can be put in low-power mode.

Transistors as switches is a wee bit involved, but a low-math discussion with good coverage of load resistors and base resistors is here:
How to Operate a Transistor as a Switch - Technical Articles (eepower.com)

Here’s what I have so far… Hopefully it’s not too confusing. I forgot to mention that I also have a LIS3MDL magnetometer and a PMS7003 particulate matter sensor connected as well

Sorry I also forgot to include in the schematic that I have a 5V to 3V3 logic level shifter between the SDA and SCL pins of the BMP581 and the SDA/SCL bus of the Arduino. It’s also connected between the Tx pin of the PMS7003 and the Rx2 pin of the Arduino

The barrel jack outputs 9V and 1A. I haven’t measured the current draw of each module and the total current draw yet, but I have been planning to. I’ll try to do it tomorrow and post the results back here

I was thinking I could just call the init function for each sensor after turning it on with the transistor. Would that not work?

You've got 5 devices on i2c, may have pullup issues
Don't use the 3V3 from then mega. Disconnect that device for test.
If you have enough 5V handy, drop the 9V PS from the barrel jack, and run the 5V in on the +5V pin.
If you power bounce devices, you'll develop nightmares debugging.
Get a big PS for test, one that has A's to spare to eliminate crappy power as an issue. Then, when everything else is working, you can measure the actual draw and select an appropriate PS from there.
My projects use a 32x8 LED matrix display that, by itself, can draw close to 2A with all LED's on at full brightness! 20mA here, 40mA there, it all adds up. Some cards, like an SD reader, can draw over 125mA during writes. So you need at least 25% headroom on your PS spec for reliable operation.
. . . . . .
PS: I'll bet that GPS module demands some significant current ...

I would first measure the running power consumption, then decide if you need all the power saving stuff.

Apart from using a device such at the TPL5110 to power off and sleep the entire project, there is no universal method of powering stuff and poweing down some I2C devices can stop the I2C bus from working.

1 Like

I'd look at it like this: either you are going to run that system on batteries or you are going to run it using a mains adapter.
Designing a low power system starts with a low power board, that is without voltage regulator, leds, USB/uart chip etc., using sleep mode, scheduling power on/off of sensors/modules as required either using arduino pins as a power source, transistors as switches, modules' own shutoff feature etc. etc. But, anyway, if you want the display on all the time, that more or less rules out battery operation.
If you don't want to go that far, then why not retain the mains power adapter and simply solve the problems causing the display to dim etc. Work out the total power those modules could consume (worst case), dimension the power supply appropriately, and ensure the wiring is sound.

I don't see anything 'power hungry' on your list, so do a power budget(using specs for all devices) to see if you're anywhere near your 2A. I'd be surprised if you're above 1A. So either your 5V source is 'optimistically' rated, or you've got circuit problems. Have you actually measured the 5V rail when your problems are evident?
Is all of this on a breadboard still? If so, change your strategy for your ground and 5V distribution immediately. Breadboards are not power distribution centers, despite the convenience of doing so. Instead, cobble up a couple of pieces of perfboard with screw terminal busses and do your power distribution that way. Put your larger capacitors from your sketch on that board, and smaller at the individual devices. That's what I'd be looking at, anyway.

At this point a photo of the assembly is in order.

Hypothetically, it sounds reasonable, but POR (power-on reset) internal to the sensor will present an issue with just how long must the Arduino wait after the power-on signal until the sensor is stabilized and ready to be initialized from the Arduino.

If you have five I2C devices, each with its own pullup resistors, plus level shifters on some, each with two pullup resistors, the total parallel resistance may be too low to get reliable I2C performance. You might look at using an I2C multiplexer that lets you eliminate the level shifters and the pullup resistor problems.

https://www.adafruit.com/product/2717

Also, if you turn off the power to a device, remember that some may have protection diodes from their I/O pins to their Vcc pins. If you power a device down, then its Vcc pin will drift down to ground, at which point you have a diode to ground on the I/O pin. So if you cut power, you also must make sure that any other line going to that device is also low, or tristate, but not high. If it's an I2C device, you may not be able to power it down without messing up the I2C bus.

That said, for devices which have low currents (a few milliamps), you might be able to power them directly from a Mega GPIO pin, without needing a mosfet. But for mosfets, the TP2104 is a TO-92 P-channel mosfet that would probably work for most things up to a few hundred milliamps.

Sorry for the late reply, I couldn’t find time to work on the project until the weekend. But anyway, I made another bread board ready USB cord and connected it to the sensor bank’s power supply rail. So now the sensors have 4A available. This definitely fixed the LCD dimming and Arduino crashing issues and the project is running much better now.

With the 4A supply connected and the sketch running, I measured the current consumption of each module:

SHT31-D: 0.249-0.690 mA
PMS7003: 30-56 mA
MQ-2: 110.8 mA
BMP581: 1.4-3.1 mA
Photoresistor: 0.35 mA
Microphone: 0.269 mA
GPS: 25.5 mA
I2C LCD: 33.5 mA

I haven’t connected the ESP-01 and magnetometer yet so I wasn’t able to measure them. But so far the biggest current draws are from the PMS7003, MQ-2, and GPS. Luckily for me, all three of those devices communicate with the Arduino via serial. So I think what I will do is use transistors to switch off those devices when not in use. That way I can avoid any I2C issues arising from powering on and off the devices. The low current I2C devices are probably not worth messing with.

Also, I’ve decided to upgrade the project with a 2.8” TFT display which is connected with SPI. So that will take one device off of my I2C bus.

I think you’re right… As you can see from the pic below it’s quite the rat’s nest at this point. I’m sure the breadboard and jumper wire connections have lots of resistance and are contributing to the overall power consumption. I did notice an improvement when I moved the PS caps closer to the raw supply input.

Once the coding is done, I’m going to move everything to a more compact prototyping shield where everything will be soldered. So hopefully that should reduce some of the loss of power.

Thanks for the part number of the MOSFET! I just ordered 10 pcs from DigiKey. As someone mentioned earlier that turning on and off I2C devices could cause connectivity problems, I have decided to only switch the higher current devices (PMS7003, MQ-2, and GPS). Luckily these are all connected via hardware serial so hopefully there won’t be any issues with that.

I purchased a few lab power supplies a few years back, I have both analog and digital versions. They meter both current and voltage with both being adjustable. I have more then paid for them by not damaging my projects. I also very seldom use prototype boards as the connections are not reliable. I also do a full blown schematic before I start and check current requirements etc, I find most of my mistooks on the schematic before assembling. Most of the time if there is a problem it is a miswire.