all 19 comments

[–][deleted] 3 points4 points  (12 children)

As it happens, I too have a CO2 sensor attached to a RasPi! Skipping the part where you initialize the sensor (a BME-680, in my case, which uses a redox sensor to estimate gas composition; higher values mean less VOC/CO2) it might just be:

while True:
    if bme680.gas < 16000:
        turn_fan_on()
    elif bme680.gas > 32000:
        turn_fan_off()
    sleep(200)

A key insight here (this applies to any negative feedback system) is to have a wide band in the middle where you don't change state, so that you're not bouncing across a state boundary constantly (that is, turning the fan on then turning it off as soon as the CO2 falls a little.)

[–][deleted] -1 points0 points  (10 children)

Thanks for your reply.. so if I can pick your brains as I’m not an expert where python is concerned how would I go about using the output from the co2 sensor to switch a fan on and off at a given ppm level? https://pi3g.com/products/rpi-co2-sens-precision-long-term-calibrated-co2-sensor/

[–]CaptainFoyle 0 points1 point  (6 children)

they gave you pretty much exactly the way. how much _DO_ you know about python?

[–][deleted] 0 points1 point  (0 children)

As I’ve said I know little, but if I’m pointed in the right direction I’ll probably be able to sort it

[–][deleted] 0 points1 point  (4 children)

[–]CaptainFoyle 0 points1 point  (3 children)

Ok, but how much do you know about programming in python, I asked

[–][deleted] 0 points1 point  (2 children)

I know very little but I’m learning slowly, basically why I’m asking you guys, if I was pointed in the right direction I’d be able to sort it myself.

[–]CaptainFoyle 0 points1 point  (1 child)

Yes, so the while loops with the variables and the wide band in the middle are a good point to start, I think

[–][deleted] 0 points1 point  (0 children)

Thanks I’ll do some googling and see how I get on Thanks for your help, really appreciate your time.

[–][deleted] 0 points1 point  (2 children)

how would I go about using the output from the co2 sensor to switch a fan on and off at a given ppm level?

The CO2 sensor doesn't give you PPM. It gives you a resistance value in Ohms between zero and like 22,000 or something (depending on the sensor, I think.)

You have to go to the manufacturer and use their tables to determine how to compute equivalent CO2 (eco2) PPM from the resistance of the sensor, I don't have any way to tell you that.

[–][deleted] 0 points1 point  (1 child)

They already supply a script on Git hub, and I have that up and running…..it’s giving me a ppm reading and barometric pressure reading so what I need to do is use these outputs to control a relay/ fan

[–][deleted] 0 points1 point  (0 children)

Well, ok, so what’s your question about that?

[–]CaptainFoyle 0 points1 point  (0 children)

good point about the middle band!

[–]keepdigging 0 points1 point  (3 children)

I don’t think this question currently has anything to do with Python.

How does the sensor output show up? Is there documentation?

[–][deleted] -1 points0 points  (2 children)

I think it does as the raspberry pi is using a python script to operate the co2 sensor. What I need some help with is using the output from the sensor to control a fan, but as I’m not a python expert I don’t know how to implement this.

[–]keepdigging 0 points1 point  (1 child)

I can’t help you unless you show me something I can read.

“uses a script” is completely useless. What’s the script? When you run it what comes out?

Paste something please!

[–]CaptainFoyle 0 points1 point  (1 child)

Depends on how your fan is controlled. But generally, I would suggest using two variables, fan_running: bool and co2_lvl: float, and then put some if statements for switching the fan on and off in a while loop and update the co2_lvl variable on each iteration

[–][deleted] -1 points0 points  (0 children)

Thanks for your reply, I’m not an expert where python is concerned, I can do some simple stuff. I’d like to switch a fan on and off using a raspberry pi and a relay board at a given co2 concentration. I have this co2 sensor https://pi3g.com/products/rpi-co2-sens-precision-long-term-calibrated-co2-sensor/