# Control variable external power supply with Arduino

I'm currently working on an air pollution controller project and I have a problem to control an external power supply to supply 5V, 1.5V or 0V to an MQ7 gas sensor.

Using the direct PWM output to power the MQ7 is a bad idea because it drains 150mA at 5v for heating. I planned to control the voltage through a NPN transistor. The voltage of 5V and 1.5V are important to be sure that the output values are consistent.

I started with Experiment 1 which was to obvious way of achieving it for me. The PIN 2 (PWM) controls the S8850 NPN transistor.

With Experiment 1:

• Voltage measured at points A and B.
• Without the MQ7, the voltage was just great (5V, 1.5V and 0V depending on analogWrite() values)
• With the MQ7, the voltage dropped to 3.8V, 1.1V and 0V with the same analogWrite() values)

So, with Experiment 2 using an external power supply (maybe the Arduino cannot manage the drain of 150mA which leads to a decrease of voltage...)

With Experiment 2:

• External supply for the MQ7.
• Voltage measured at points A and B.
• With the MQ7, the voltage is correct (5V, 1.5V and 0V with the same analogWrite() values) as long as I provide 8V on the external power supply.

For the record, a snippet of the code :

const int PIN_MQ7_POWER = 2;

void setup() {
// Prepare serial console
Serial.begin(9600);

// Prepare heater/power MQ7
pinMode(PIN_MQ7_POWER, OUTPUT);
}

void loop() {
Serial.println("Switch to 5v");
digitalWrite(PIN_MQ7_POWER, HIGH);
delay(10000);

Serial.println("Switch to 1.5v");
analogWrite(PIN_MQ7_POWER, 255/5*1.5);
delay(10000);

Serial.println("Switch to 0v");
digitalWrite(PIN_MQ7_POWER, LOW);
delay(10000);
}


I checked the internet and all schematics use direct output of the Arduino to power the MQ7 (through PWM). IMHO, it could work for an experiment of some cycles in the lab, but not for a long-term solution.

The usage of an 8V additional power supply is not convenient and I'm pretty sure that it is possible to have a better schematics to select the 3 different voltages easily from the Arduino.

Any ideas how to achieve it?

• Using a low-side switch, especially BJT, may cause logic level problem. – Long Pham Sep 13 '18 at 17:07
• Are you powering the Arduino from USB? – JRE Sep 13 '18 at 17:47
• @JRE : I’m powering the Arduino from 9v on the jack. USB is only there to monitor the serial console. – ostaquet Sep 13 '18 at 17:55
• @LongPham : what do you mean with logic level problem? – ostaquet Sep 13 '18 at 17:56
• 9V as in "9V transistor battery?" If so, then that's your problem. – JRE Sep 14 '18 at 16:48