Sense a voltage and turn on a pin

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Ridley Walker
Posts: 19
Joined: Thu Jun 25, 2015 1:04 am

Sense a voltage and turn on a pin

Post by Ridley Walker » Thu Jul 21, 2016 7:01 pm

i want to control temperature in a small greenhouse. I have a proper sensor ordered, but in the meantime, i have been hacking around with a simple resistor/thermistor string between 3.3v and ground. I can sense the voltage on ADC1, print it, and blink an led once a second.
I have been trying use an " if ADC is > do something" "else if ADC is < do something" where "do something" is turn a pin on or off, etc
I have used every iteration "while" " if > ,if < " that i can think of. I have run out of ideas.

If anybody could point me to a code snippet that will help me get this done, i will be most appreciative.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Sense a voltage and turn on a pin

Post by dhylands » Thu Jul 21, 2016 7:11 pm

I'd highly recommend that you take a free python course. I think the gist of what you're looking for is along these lines:

Code: Select all

while True:
    adc_value = read_adc_value()
    if adc_value > upper_threshold:
        set_gpio_high()
    elsif adc_value < lower_threshold:
        set_gpio_low()
This web page has lots of examples: https://docs.python.org/3/tutorial/index.html
If you want something a bit more structured like a course, then this udacity course is reasonable:
https://www.udacity.com/course/intro-to ... nce--cs101

Ridley Walker
Posts: 19
Joined: Thu Jun 25, 2015 1:04 am

Re: Sense a voltage and turn on a pin

Post by Ridley Walker » Thu Jul 21, 2016 11:53 pm

thanks Dave,
I have been looking for a course that is "beginner " enough for me. I was going thru a course from Code Academy, I think it was, but it disappeared! I'll have to get more serious looking for it...

In your code you referenced "elsif". Did you mean "elif"?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Sense a voltage and turn on a pin

Post by dhylands » Fri Jul 22, 2016 1:17 am

Ridley Walker wrote:In your code you referenced "elsif". Did you mean "elif"?
Yeah - I work in a bunch of different programming languages on a more or less daily basis, so sometimes I get things confused.

Ridley Walker
Posts: 19
Joined: Thu Jun 25, 2015 1:04 am

Re: Sense a voltage and turn on a pin

Post by Ridley Walker » Fri Jul 22, 2016 2:41 pm

Confused? Confused!!! You have no idea.
Imagine me at 76 yrs old, can't remember what i had for breakfast, zero experience with this stuff.

Thanks again, your code fixed me right up. I used your code to show me the errors in the stuff i had cobbled up.
Now a happy camper - my greenhouse will be warm this winter...

41536172@qq.com
Posts: 11
Joined: Sun May 08, 2016 3:41 pm

Re: Sense a voltage and turn on a pin

Post by 41536172@qq.com » Tue Aug 16, 2016 2:21 pm

and you also need to know about "PID"control.

Post Reply