LED blinking and DHT22 Sensor Codes

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
kpin404
Posts: 13
Joined: Mon Jun 21, 2021 11:56 am

Re: LED blinking and DHT22 Sensor Codes

Post by kpin404 » Mon Aug 02, 2021 12:07 pm

davef wrote:
Mon Aug 02, 2021 9:29 am
Firstly, sorry I see you do call sensor.temperature() and sensor.humidity()

It looks you have another file called main.py. Better post that as I don't understand:

Code: Select all

Ready to download this file,please wait!
...
download ok
exec(open('./main.py').read(),globals())
I understand that you want both of these to execute in the one loop, just one after another.
Yes! you are right i want to execute in an one loop or you can say to utilize ESP32 with two more functions within one programming code loop , just one after another. This is just for to go one step further to play and practice with the learnt codes from a simple to a little bit more complex.

Sorry, but there is only one main.py file i have created and trying to execute two different programs within one main.py. The complete main.py code:

Code: Select all

from machine import Pin
from dht import DHT22
from time import sleep

led = Pin(2, Pin.OUT)
sensor = DHT22(Pin(14)) 
while True:
	sensor.measure()
	print('Temperature = %.2f' % sensor.temperature())
	print('Humidity = %.2f' % sensor.humidity())
	sleep(3)
  
	led.on()
	sleep(0.5)
	led.off()
	sleep(0.5)	
When i upload and run the code within uPyCraftV1.1 IDE to ESP32 REPL shell starts with >>> ready to download this file, please wait... it waits... and says download OK and run the program without any error message and print the temperature and humidity values every 3 seconds at the Output:
>>>

Ready to download this file,please wait!
...
download ok
exec(open('./main.py').read(),globals())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 7, in <module>
NameError: name 'dht' isn't defined
>>>
>>>

>>>

Ready to download this file,please wait!
...
download ok
exec(open('./main.py').read(),globals())
Temperature = 25.60
Humidity = 87.90
Temperature = 25.90
Humidity = 87.60


Sensor code is working fine, but LED blinking code is not making to blink the LED as per its code. Thanks.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: LED blinking and DHT22 Sensor Codes

Post by davef » Mon Aug 02, 2021 8:41 pm

OK about the IDE. I just use the command line so didn't recognise what was happening.

So, back to my earlier comment about removing the DTH22 stuff and just focusing on getting the LED to work.

Code: Select all

from machine import Pin
from time import sleep

led = Pin(2, Pin.OUT)

while True:
	led.on()
	print('LED on')
	sleep(0.5)
	led.off()
	print('LED off')
	sleep(0.5)	
What happens?

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: LED blinking and DHT22 Sensor Codes

Post by davef » Mon Aug 02, 2021 11:34 pm

This link looks exactly what you need, uPyCraft IDE and blinking a LED
https://randomnerdtutorials.com/getting ... 2-esp8266/

kpin404
Posts: 13
Joined: Mon Jun 21, 2021 11:56 am

Re: LED blinking and DHT22 Sensor Codes

Post by kpin404 » Tue Aug 03, 2021 2:26 am

davef wrote:
Mon Aug 02, 2021 8:41 pm
OK about the IDE. I just use the command line so didn't recognise what was happening.

So, back to my earlier comment about removing the DTH22 stuff and just focusing on getting the LED to work.

Code: Select all

from machine import Pin
from time import sleep

led = Pin(2, Pin.OUT)

while True:
	led.on()
	print('LED on')
	sleep(0.5)
	led.off()
	print('LED off')
	sleep(0.5)	
What happens?
Thanks for keep supporting. No doubt LED blinking code is working fine when execute singly. I have run your provided code and the output:
>>>
Ready to download this file,please wait!
..
download ok
exec(open('./main.py').read(),globals())
LED on
LED off
LED on
LED off
LED on
LED off
LED on
LED off
LED on
LED off
But, when added LED blinking code with other codes it doesn't work at all.

kpin404
Posts: 13
Joined: Mon Jun 21, 2021 11:56 am

Re: LED blinking and DHT22 Sensor Codes

Post by kpin404 » Tue Aug 03, 2021 2:41 am

I have modify the code:

Code: Select all

from machine import Pin
from time import sleep

led = Pin(2, Pin.OUT)
sensor = dht.DHT22(Pin(14))

while True:
	led.on()
	print('LED on')
	time.sleep(0.5)
	led.off()
	print('LED off')
	time.sleep(0.5)	
	
	sensor.measure()
 	print('Temperature = %.2f' % sensor.temperature())
	print('Humidity = %.2f' % sensor.humidity())
	time.sleep(3)
Output:
>>>

Ready to download this file,please wait!
...
download ok
exec(open('./main.py').read(),globals())
LED on
LED off
Temperature = 24.40
Humidity = 87.90
LED on
LED off
Temperature = 24.20
Humidity = 89.30
LED on
LED off
Temperature = 24.20
Humidity = 89.30
LED on
LED off
Temperature = 24.20
Humidity = 89.30
LED on
LED off
Temperature = 24.20
Humidity = 89.40
LED on
LED off
Temperature = 24.20
Humidity = 89.40
LED on
LED off
Temperature = 24.20
Humidity = 89.40
LED on
LED off
Temperature = 24.20
Humidity = 89.40
LED on
LED off
Temperature = 24.20
Humidity = 89.30
LED on
LED off
Temperature = 24.20
Humidity = 89.30
But, now the next problem is that LED is blinking @3 second instead of @0.5 second as per its own code timing. Why its taking sleep function from sensor interval rather than its own sleep coded function? Thanks.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: LED blinking and DHT22 Sensor Codes

Post by davef » Tue Aug 03, 2021 3:18 am

Your code says to turn the LED on for 0.5 seconds and off for 3.5 seconds.

If you want the LED to keep blinking 0.5 seconds on and 0.5 seconds off and allow 3 seconds for sensor timing, then I suspect you need to follow Peters advice on uasynio.

In my applications I use the LED as a "heartbeat" and then if the rest of the loop takes several seconds I am happy with that.

Code: Select all

 #  heartbeat, this also slows the loop down
    pin14.on()
    utime.sleep_ms(250)
    pin14.off()
    utime.sleep_ms(250)

kpin404
Posts: 13
Joined: Mon Jun 21, 2021 11:56 am

Re: LED blinking and DHT22 Sensor Codes

Post by kpin404 » Tue Aug 03, 2021 3:28 am

davef wrote:
Tue Aug 03, 2021 3:18 am
Your code says to turn the LED on for 0.5 seconds and off for 3.5 seconds
Okay! Go it.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: LED blinking and DHT22 Sensor Codes

Post by davef » Tue Aug 03, 2021 3:32 am

I see I mis-understood your original posting. I thought that you could not get the LED to blink at all, but could get temp and humidity readings.

Sorry, for creating more confusion.

kpin404
Posts: 13
Joined: Mon Jun 21, 2021 11:56 am

Re: LED blinking and DHT22 Sensor Codes

Post by kpin404 » Tue Aug 03, 2021 3:39 am

davef wrote:
Tue Aug 03, 2021 3:32 am
I see I mis-understood your original posting. I thought that you could not get the LED to blink at all, but could get temp and humidity readings.

Sorry, for creating more confusion.
Its okay! No problem at all. Could you please let me assist how to use uasyncio to code for this? Thanking you.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: LED blinking and DHT22 Sensor Codes

Post by davef » Tue Aug 03, 2021 3:54 am

I have not needed to use it and therefore in no position to advise you.

I have read through Peter's tutorial in preparation for the day that I might need to use it. I think it would be fairly easy to do a simple application like yours. I would expect a simple example like yours in a tutorial.

Standard Python uses asycnio. These people produce good tutorials https://realpython.com/async-io-python/

I'll read through it myself as a learning opportunity.

Good luck

Post Reply