# Connecting 5V devices to Photon?

**URL:** <https://community.particle.io/t/connecting-5v-devices-to-photon/24081>\
**Category:** Troubleshooting\
**Created:** [July 2, 2016, 10:16am UTC](https://community.particle.io/t/connecting-5v-devices-to-photon/24081 "2016-07-02T10:16:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilak2k](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ilak2k/32/9992_2.png) [@ilak2k](https://community.particle.io/u/ilak2k)\
**Post date:** [July 2, 2016, 10:16am UTC](https://community.particle.io/t/connecting-5v-devices-to-photon/24081/1 "2016-07-02T10:16:45Z")

</div>

I’m connecting a number of 5V devices to the Photon and reading outputs via Analog Pins…

Is there a general schematic & set of components to convert the 5V analog output to 3.3V, thus avoiding damage to the Photon’s ADC & get more usable readings?

I used an LDR, connected to the Vin Pin of Photon, to calculate Ambient Light. And the photon never read 0 when there is no light. In a range of 0-100, the minimum photon could compute is 12-13. Then I powered the LDR with 3.3V Pin of Photon. And now it shows 0 when there’s no light. Now my device is safe and I get better usable readings.

This makes me think converting the Analog Output from 5V devices to 3.3V will solve a lot of issues. In my case I use a MQ135, MQ6 sensors and they need to be powered with 5V(using Vin) so they get heated up and work correctly. But now the output of these devices has to be converted to 3.3V.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [July 2, 2016, 10:50am UTC](https://community.particle.io/t/connecting-5v-devices-to-photon/24081/2 "2016-07-02T10:50:53Z")

</div>

For the MQ135 this is already done via the load resistor `Rl` which builds a voltage divider that can be calculated via _Ohm’s Law_.  
You just need to choose an `Rl` **_less than_** the minimal expected `Rs` times `2` (`Rl < 2 * Rs,min`).  
`Rl = 2*Rs,min` would give you a voltage drop of `Vcc * 1/3` across `Rs` leaving you with `Vcc * 2/3` on your sensor pin (`5V * 2/3 = 3.3333V` - just a bit too much, hence _less than_! Best with some margin for tolerance of resistance, voltage and temp drift).

If you have other sensors that actively output up to 5V (and you don’t know the output impedance) you might want an OpAmp circuit with a voltage devider in the feedback loop.

like here

> **[Operational amplifier applications | Non-inverting amplifier](https://en.wikipedia.org/wiki/Operational_amplifier_applications#Non-inverting_amplifier)**
>
> A non-inverting amplifier is a special case of the Differential amplifier in which that circuit's inverting input V1 is grounded, and non-inverting input V2 is identified with Vin above, with R1 ≫ R2. Referring to the circuit immediately above,

---

<div class="post-metadata">

**Author:** ![ilak2k](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ilak2k/32/9992_2.png) [@ilak2k](https://community.particle.io/u/ilak2k)\
**Post date:** [July 2, 2016, 11:07am UTC](https://community.particle.io/t/connecting-5v-devices-to-photon/24081/3 "2016-07-02T11:07:43Z")

</div>

> [@ScruffR](#):
>
> For the MQ135 this is already done via the load resistor Rl which builds a voltage divider that can be calculated via Ohm's Law.You just need to choose an Rl less than the minimal expected Rs times 2 (Rl \< 2 \* Rs,min).Rl = 2\*Rs,min would give you a voltage drop of Vcc \* 1/3 across Rs leaving you with Vcc \* 2/3 on your sensor pin (5V \* 2/3 = 3.3333V - just a bit too much, hence less than! Best with some margin for tolerance of resistance, voltage and temp drift).

I have the MQ135 from this link: [http://www.ebay.in/itm/152139272580](http://www.ebay.in/itm/152139272580)

This is not the bare MQ135 sensor alone. The sensor is mounted on a board with Vcc, Gnd, D0 & A0. It's got some SMD resistors and it has a variable resistor. But VR is only to set a threshold trigger a digital high or low via D0...

So, for me to choose the Rl, I need to know the Rs  
Rs - Is this the resistance at the analog output?  
When I measured impedance between sensor's analog output and ground, my fluke digital multimeter measured 1K Ohm. Is this how calculate Rs?

How do I calculate/know the minimal expected Rs?  
Rl - Where do I connect the Rl?

Can you suggest a wiring diagram, for MQ135 mounted on a PCB?

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [July 2, 2016, 12:44pm UTC](https://community.particle.io/t/connecting-5v-devices-to-photon/24081/4 "2016-07-02T12:44:52Z")

</div>

Your board looks somewhat like this one.  
[http://www.sunrom.com/p/air-quality-sensor-mq135](http://www.sunrom.com/p/air-quality-sensor-mq135)

Where `R2` (102 = 1k) corresponds to `Rl` and is not adjustable, so you you’d need to take other steps like adding your own voltage divider with two resistors 1R + 2R to bring 5V down to 3.3V or better add your voltage follower.

On the other hand the [MQ135 datasheet](https://dl.dropboxusercontent.com/u/87066238/MQ135.pdf) states a range for `Rs` of 30k~200k and a recommended `Rl`of 10k~47k.  
But with 1k for `Rl` you’d be looking at a max voltage of around 0.16V (😕 ??? 😕)  
Something seems fishy there 😝

But since I haven’t got any of these sensors at hand I’m only theorizing 😉  
With one at hand I could get some real measurements.

* * *

Maybe you can get a voltage measurement between analog out and GND when you put the sensor in a “pure”/saturated atmosphere of any of the detectable gasses.

---

<div class="post-metadata">

**Author:** ![ilak2k](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ilak2k/32/9992_2.png) [@ilak2k](https://community.particle.io/u/ilak2k)\
**Post date:** [July 3, 2016, 5:54am UTC](https://community.particle.io/t/connecting-5v-devices-to-photon/24081/5 "2016-07-03T05:54:06Z")

</div>

> [@ScruffR](#):
>
> Maybe you can get a voltage measurement between analog out and GND when you put the sensor in a "pure"/saturated atmosphere of any of the detectable gasses.

Its exactly the same as you've mentioned at link [Air Quality Sensor - MQ135 [4600] : Sunrom Electronics](http://www.sunrom.com/p/air-quality-sensor-mq135)

So, I've added a voltage divider with resistors R1 = 820 Ohms, R2 = 1470 Ohms for stepping down 5V to 3.21V (couldn't find 500 and 1000 ohm resitors for a perfect 3.33V)

**Voltage & RAW Analog Read, without voltage divider:-**  
.34, 1540 - Pure Air Outside  
.48V, 1600 - Normal Room Condition  
1.8V, 3325 - Cigarette Lighter LPG for a minute  
2.3V, 4080 - Incense Stick Smoke, placed 1cm below sensor, for 3 minutes  
2.35V, 4095 - Incense Stick Smoke, placed 1cm below sensor, for 3 minutes

At 2.3V, analog read's value is 4080, at 2.35V, analog read's value peaks at 4095  
And voltage kept raises up to 2.5 max, that's not much after it reaches peak value right?  
Technically, shouldn't analog read value of 4095 happen only at 3.3V?

**Voltage & RAW Analog Read, with voltage divider:-**  
.18V, 1350 - Pure Air Outside  
.33V, 1580 - Normal Room Condition  
1.1V, 2450 - Cigarette Lighter LPG for a minute  
1.6V, 3154 - Incense Stick Smoke, placed 1cm below sensor, for 3 minutes

I'm still dealing only with voltage & raw analog reads here. So, once we're done with this, please guide me to translate these to ppm values 😃

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [July 3, 2016, 6:27am UTC](https://community.particle.io/t/connecting-5v-devices-to-photon/24081/6 "2016-07-03T06:27:41Z")

</div>

> [@ilak2k](#):
>
> 2.35V, 4095

That is odd.  
Can you also take a voltage measurement between 3V3 pin and GND?  
How are you powering the sensor and the Photon?  
Could you have a look with an oszilloscope how smooth your supply DC is?

---

<div class="post-metadata">

**Author:** ![ilak2k](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ilak2k/32/9992_2.png) [@ilak2k](https://community.particle.io/u/ilak2k)\
**Post date:** [July 3, 2016, 7:19am UTC](https://community.particle.io/t/connecting-5v-devices-to-photon/24081/7 "2016-07-03T07:19:54Z")

</div>

> [@ScruffR](#):
>
> That is odd.Can you also take a voltage measurement between 3V3 pin and GND?How are you powering the sensor and the Photon? Could you have a look with an oszilloscope how smooth your supply DC is?

Voltage between 3V3 and GND is 3.329 volts  
Voltage between Vin and GND is 4.83 volts

I'm powering photon, via its Micro USB port with an Aukey Charger. Like the one from here: [http://www.amazon.in/Aukey-Desktop-Charging-Station-Charger/dp/B00VHHIUNY?ie=UTF8&psc=1&redirect=true&ref\_=oh\_aui\_detailpage\_o02\_s01](http://www.amazon.in/Aukey-Desktop-Charging-Station-Charger/dp/B00VHHIUNY?ie=UTF8&psc=1&redirect=true&ref_=oh_aui_detailpage_o02_s01)

And I'm using the green port of the charger, that provides 5V/2.4A max!

And I'm powering the sensor using the Vin pin of photon itself...

No, I don't have an oscilloscope, at the moment. Is there anyway I can check my DC Supply without it?

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [July 3, 2016, 1:45pm UTC](https://community.particle.io/t/connecting-5v-devices-to-photon/24081/8 "2016-07-03T13:45:34Z")

</div>

@ilak2k, @ScruffR, adding the voltage divider in PARALLEL to the existing sensor/resistor divider will only behave as expected IF the resistor values are greater than the existing resistor values. Putting two resistors in parallel will always produce a resistance of less than the smallest resistor. So, you can increase the resistor values or add a voltage follower (op-amp) and THEN add the voltage divider.

As for the 2.35v yeilding a count of 4095 on the ADC, can you please show your sampling code? The values tend to suggest a ripple voltage you wont’ be able to measure with your multimeter in DC mode. Try putting it on AC mode, low voltage, to see if an AC ripple exists. Chargers can be a source of frustration so if you can test with another supply, it would provide a good comparison. 😀

---

<div class="post-metadata">

**Author:** ![ilak2k](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ilak2k/32/9992_2.png) [@ilak2k](https://community.particle.io/u/ilak2k)\
**Post date:** [July 4, 2016, 8:01am UTC](https://community.particle.io/t/connecting-5v-devices-to-photon/24081/9 "2016-07-04T08:01:39Z")

</div>

> [@peekay123](#):
>
> @ilak2k, @ScruffR, adding the voltage divider in PARALLEL to the existing sensor/resistor divider will only behave as expected IF the resistor values are greater than the existing resistor values. Putting two resistors in parallel will always produce a resistance of less than the smallest resistor. So, you can increase the resistor values or add a voltage follower (op-amp) and THEN add the voltage divider.

Will replace resistors and take readings again...

> [@peekay123](#):
>
> As for the 2.35v yeilding a count of 4095 on the ADC, can you please show your sampling code?

Sampling Code for measuring the analog pin:

```auto
void setup() {
    // Serial Monitor
    Serial.begin(9600);
    // Not defining pinModes for LDRPin, SmokePin, FirePin as Photon takes INPUT mode by default
    // Blynk Service
    Blynk.begin(auth);
}

void loop() {
    Particle.process();
    if (WiFi.ready()) {
        Blynk.run();
    }
int smokelevelraw = analogRead(SmokePin);
Serial.println(smokelevelraw);
}

```

I'm reading MQ135's analog out with this code and also on Blynk, with Analog Pin A5, without any range mapping, reads value as is...

> [@peekay123](#):
>
> The values tend to suggest a ripple voltage you wont' be able to measure with your multimeter in DC mode. Try putting it on AC mode, low voltage, to see if an AC ripple exists.

Switched to AC Mode and measured between Ground and Analog Out... Reads 0.003V AC

> [@peekay123](#):
>
> Chargers can be a source of frustration so if you can test with another supply, it would provide a good comparison. 😀

I have my PC's USB Port and the Aukey Charger... With Sony & Amkette Chargers, I receive slight electric shock from the pins and exposed metal parts of the circuit 😕. Sometimes to the point I couldn't touch the exposed pins. So I stopped using these and had to stick with the Aukey, for now... Aukey is clean, shock wise
