How to Store Sensors data into SD card using FreeRTOS
We have already covered how to use ADC, DHT11, SD CARD, and FreeRTOS. Today in this tutorial, we will combine them all. We will read the data from Potentiometre, which is connected via ADC, also read from DHT11 temperature sensor, and we will write this data to the SD card. This entire process will take place in different tasks of FreeRTOS.
Let’s start by setting up the Cubemx first
Above is the setup for ADC, where we will use channel 1. TIM7 is used for microseconds delay, which will be needed for the DHT11 sensor to operate. TIM1 is used as a periodic delay, where an interrupt will be triggered every 2 seconds. We will use this interrupt to release a semaphore for DHT11 task to operate.
Other than the above setup, we also need to turn on the FATFS, FreeRTOS, and SPI. You can download the code and check the *.ioc file for more setup related information, or watch the video linked below for explanation of this setup used.
Some more Setup is required with the libraries, so i would advise you watch the video below.
Some insight into the CODE
Above, i have created variables to store ADC and Temperature Data. Also, three task handlers have been defined along with 1 semaphore handler.
Inside the main function, I am creating the files, where the data will be updated later by the tasks.
Now we will create the semaphore, and the three tasks. Here, SDCARD_Task is given highest priority, because we don’t want any other task to preempt it, while the task is accessing the SD CARD. In that case, a failure of drive might occur.
Start the TIM7 normally, and TIM1 in interrupt mode. This is because the TIM1 will generate the periodic interrupt in every 2 seconds.
And finally we will start the scheduler.
ADC_Task will run every 500 ms. Where it will store the ADC value in the variable (ADC_VAL) using PollForConversion method.
DHT_Task will run only after acquiring the semaphore. The semaphore will be released by the TIM1 periodically every 2 seconds. After acquiring the semaphore, DHT11_Get_Data function will store the Temp and Rh values in the Temperature and Humidity variables.
The semaphore part is just to bring variation in this program. Ofcourse you can just use the vTaskDelay to run this task every 2 seconds.
SDCARD_Task will run every 1 second, and update the data in the files. indx variable is used for indexing.
You can see above that two files were created in the SD CRAD, and the recorded data is written to the files. For more details, watch the video below
- How to communicate between HC-12 using STM32 October 5, 2020
- How to use STM32 as a KEYBOARD September 28, 2020
- Emulate STM32F103 as a MOUSE September 8, 2020
- Send and Receive data from the WebServer using STM32 August 8, 2020
- Interface BMP180 with STM32 July 26, 2020
- STM32 USB HOST HID July 21, 2020