DHT11 || DHT22 || DS18B20 with STM32 using TIMER Delay

preview_player
Показать описание
________________________________________________________________________________________

Interface DHT11, DHT22, and DS18B20 sensors with STM32. All these sensors use one wire to communicate.

*********************************UPDATE ***************************
Those who are not able to get the DHT11 and DHT22 values, Here is another method you can use. This one is unified for both the sensors. No setup needed for timer and all. Just select the data pin as output and you are done. you need to select the DHT TYPE in DHT.c

Give at least 3 seconds delay before measuring again in case of DHT11 or else it won't measure again.

COMMON UPTO :::: 4:50

DHT11 :::: 4:50

DHT22 :::: 12:50

DS18B20 :::: 19:00

For more details and to download the code, goto

________________________________________________________________________________________

******* SUPPORT US BY DONATING******

*******Join the Membership*******

Рекомендации по теме
Комментарии
Автор

Those who are having problems with the temperature value, here are few things you should do to diagnose

1. In the set_gpio_input() function, try using GPIO_PULLUP instead of NOPULL.

2. Check the microsecond delay. This is the most vital part and 90% chances are that you have problem here. You can blink some LED every 1 second using us delay.
Try the below code
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_1);
for (int i=0; i<20; i++)
{
delay(50000);
}

3. During debugging, do not halt the MCU until all the read cycles from sensor is completed. Also do not include any LED blinking codes in between the DHT functions. These are very time sensitive devices. Even few microsecond delay can cause the fault

4. Try connecting a pull up resistor (around 4.7K) to the data pin.

5. Do not use the 5V from the MCU. Try giving power from an external source (specially to DHT22). Trust me I wasted a lot of time due to this.

6. Check some comments which I have given the heart (❤️) to. Those solutions have worked for some.

7. Check the connection. I have come across few people, who were not connecting the sensor properly. They were providing VCC to the data pin.
This is very common mistake if you have the sensor without breakout board.

8. Keep trying, the code is alright and it works perfectly. I have written it as per the instructions are provided in the datasheet. Also I have tested them over and over.

ControllersTech
Автор

I have repeated your code with DHT11. At the first time it did not work at all, only DHT11_Start () function did properly and i could clearly see the response from sensor with help of osciloscope. Actually i had to extend HAL_Delay(1000) after all readings up to 1200 ms and it started working continuosly . Thank you so much for helping.

АльбинаКалабанова
Автор

I followed with DHT11 and STM32F103C8T6.
After a few trials and reading the comments, it works perfectly!

Ngigi
Автор

Hola, espero lo entiendan en español.
El problema de que no arranque el DHT11 es por el comienzo de la señal enviada por el MCU.
El sensor espera una señal en alto y luego los 18 mS en bajo. Se ve claramente en los diagramas del datasheet.
Lo solucioné agregando unas líneas en el código de DHT11_Start de la siguiente manera:

void DHT11_Start (void)
{
Set_Pin_Output(DHT11_PORT, DHT11_PIN);
HAL_GPIO_WritePin(DHT11_PORT, DHT11_PIN, 1);
/* Nivel de comienzo de comunicación */
delay(1000);
HAL_GPIO_WritePin(DHT11_PORT, DHT11_PIN, 0);
delay (18000);
HAL_GPIO_WritePin(DHT11_PORT, DHT11_PIN, 1);
delay(20);
Set_Pin_Input(DHT11_PORT, DHT11_PIN);
}

Espero les sirva. Muy bueno el posteo y gracias, me fue de muchísima ayuda.

mateaguero
Автор

i tried a pull up resistor (around 4.7K) and giving power from an external source, it works perfectly

dighuyntran
Автор

Hi sir! Could you consider making a video using these sensors without stopping the machine? I mean, it has a lot of delay that will affect the operation of the most sensitive systems. It would be interesting if you combined it with Rtos. Greetings, I've been following you for years!

ServiciosydesarrolloCO
Автор

Sensor response doesn't work at all for me, doesnt matter if you connect data pin or not, program never enters if statement in DTH11_Response, and at the end of function the code is stuck on this while loop if you dont connect the data pin I dont know what I'm doinf wrong.

radoslav
Автор

Hay, this is a very interesting video. thank's for the detailed explanation.
Can u show me how to access ds18b20 in parasitic mode.
Thank You

bimower
Автор

1st The program goes to the response waiting function if (!(HAL_GPIO_ReadPin (DHT_PORT, DHT_PIN))) then it skips because at this time GPIOA_PIN_1 is SET and it runs to the while function while((HAL_GPIO_ReadPin (DHT_PORT, DHT_PIN))); // wait for the pin to go low
and it's stuck in this loop because GPIO_PIN_1 is still SET without RESET. Even though in theory it should be low. I don't understand why it works like that but when I try to unplug the power to put the GPIO pin down low level and plug the power back in, the program works normally. It only crashes the first time the code is loaded. Do you know why?🙂🙂🙂

huythaiquang
Автор

Muchas gracias por su ayuda, lo probé con el DS18B20 en el programa STM32cubeIDE, al principio no me funcionó, y por alguna razón no corre el programa general cuando uso el HAL_delay, así que solo usé el delay() de la función creada, en este caso al ser solo para datos de 16bits, para los los retardos mas grandes usé la estructura for.

jeffalvarez
Автор

Thanks for video, but I think I need to add timeouts everywhere to not stuck somewhere in loop.

artsrunmargaryan
Автор

Thank you! Everything works great on a Nucleo32-L432KC w/o any pullup resistor and with the DHT22 powered directly from Nucleo's 3.3V VCC pin. Great explanations! Subbed! :D

cosmin
Автор

Thanks for the helpful video. I had the problem of receiving the correct temperature in the L476RG-stm32 board. But I solved it by setting "80-1" instead of "50-1" in the prescaler in the TIMER stetting of the board scheme.

amintayebi
Автор

it ran on me, but need to start timer6 with "HAL_TIM_Base_Start(&htim6);" and need pull up resistor. two sides of setup have open drain or not. i used 3.3k resistor for 3.3volt setup and ds18b20 worked 3.3v.

kpcgkhn
Автор

I had a problem, but I fixed it. I was using 8 MHz HCLK, but once I increased it to 48 MHz (plus increased prescaler on timer) everything started working fine

vandal
Автор

If u are gonna make the clock frequency 100 MHz, the DS18B20 is working correctly. The 100MHz is required for creating nanoseconds delay and please dont forget to add 4.7 K (pull up) resister to data pin.

atakan
Автор

thanks for sharing. If you don't mind, Can you make a video about max31855 for temperature measurement?

sinancetin
Автор

Code doesn't work with DS18B20; using STM32F103C8T6 (Bluepill)

xenonyoutub
Автор

does the delays have to be super precise? to test it out, i tried a 10 microsecond delay with a toggle pin and i get a pwm with 51.5 kHz frequency. Sometimes for highest delay like, 200 us, i get 2.488 khz.

ruialmeida
Автор

THankyou for this video!DS18B20 works perfect.
Can you tell how to change the resolution of sensor and will the code work for negative temperature values?

hashimahmed