Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

ESP32 Standard Library Embedded Rust: Analog Temperature Sensing using the ADC

Tags: temperature

Posted on Aug 18 This blog post is the sixth of a multi-part series of posts where I explore various peripherals in the ESP32C3 using standard library embedded Rust and the esp-idf-hal. Please be aware that certain concepts in newer posts could depend on concepts in prior posts.If you find this post useful, and to keep up to date with similar posts, here's the list of channels you can follow/subscribe to:Follow Me on TwitterSubscribe to NewsletterFollow me on GitHubClick Here for Rust Swag NTC thermistors, or Negative Temperature Coefficient thermistors, are temperature-sensitive resistors made from materials that exhibit a change in resistance with temperature variations. As the temperature increases, the resistance of an NTC thermistor decreases (thus the negative naming), and conversely, when the temperature decreases, the resistance increases. This unique property makes NTC thermistors invaluable in temperature measurement and control applications. They are widely used as temperature sensors in electronic circuits, household appliances, automotive systems, industrial processes, and medical devices. NTC thermistors enable accurate temperature monitoring, providing feedback for maintaining stable operating conditions, triggering alarms, and regulating various processes with high precision.In this post, I will be configuring and setting up an esp32c3-hal ADC using the esp-idf-hal to measure ambient temperature using a 10k (NTC) Thermistor. The ADC collected value will be converted to temperature and sent to the terminal output.To understand the content of this post, you need the following:Basic knowledge of coding in Rust.Familiarity with the working principles of NTC Thermistors. This page is a good resource.All the code presented in this post is available on the apollolabs ESP32C3 git repo. Note that if the code on the git repo is slightly different then it means that it was modified to enhance the code quality or accommodate any HAL/Rust updates.Additionally, the full project (code and simulation) is available on Wokwi here.ESP32-C3-DevKitMA 10k NTC Temperature Sensor.A 10 KOhm Resistor.If you want to wire real hardware, instead of getting the resistor and sensor separately, you can get assemblies that contain the needed setup like this one.The temperature sensor used is a negative temperature coefficient (NTC) sensor. This means the resistance of the sensor increases as the temperature increases. The following figure shows the schematic of the temperature sensor circuit:It is shown that the NTC Thermistor is connected in a voltage divider configuration with a 10k resistor. As such, the voltage at the positive terminal of the op-amp V+V_{+} V+​ is equal to the voltage on the signal terminal and expressed as:Where R1=10kΩR_1 = 10k\OmegaR1​=10kΩ and the resistance value of (RNTC(R_{\text{NTC}}(RNTC​ is the one that needs to be calculated to obtain the temperature. This means that later in the code, I would need to retrieve back the value of RNTCR_{\text{NTC}}RNTC​ from the V+V_{\text{+}}V+​ value that is being read by the ADC. With some algebraic manipulation, we can move all the known variables to the right-hand side of the equation to reach the following expression:After extracting the value of RNTCR_{\text{NTC}}RNTC​, I would need to determine the temperature. Following the equations in the datasheet, I leverage the Steinhart-Hart NTC equation that is presented as follows:where β\beta β is a constant and equal to 3950 for our NTC as stated by Wokwi and TT T is the temperature we are measuring. T0T_0 T0​ and R0R_0 R0​ refer to the ambient temperature (typically 25 Celcius) and nominal resistance at ambient temperature, respectively. The value of the resistance at 25 Celcius ( T0T_0 T0​ ) is equal to 10kΩ10k\Omega10kΩ ( R0R_0 R0​ ). With more algebraic manipulation we solve for TT T to get:Take note that the β\beta β value is typically obtained by manufacturers based on Kelvin temperatures. As such, when doing the calculations we'd have to convert back to Celcius.Now that we know the equations from the prior section, an algorithm needs to be developed and is quite straightforward in this case. After configuring the device, the algorithmic steps are as follows:Kick off the ADC and obtain a reading/sample.Calculate the temperature in Celcius.Print the temperature value on the terminal.Go back to step 1.In this implementation, the following crates are required:The esp32c3_hal crate to import the ESP32C3 device hardware abstractions.The esp_backtrace crate to define the panicking behavior.The esp_println crate to provide println! implementation.The libm crate to provide an implementation for a natural logarithm.1️⃣ Obtain a handle for the device peripherals: In embedded Rust, as part of the singleton design pattern, we first have to take the device peripherals. This is done using the take() method. Here I create a device peripheral handler named peripherals as follows:2️⃣ Configure and Obtain a handle for the ADC driver: The ADC is configured in two steps in the esp-idf-hal first by setting up the AdcDriver that configures the ADC, and second setting up the channel that will configure the channel/pin settings. From the ESP32C3 reference manual, the following table exists:The table shows which ADC channels are connected to which ADC. In this case, since we are using gpio4 , then ADC1 is the one we should be configuring. To configure ADC1, there exists the AdcDriver new method that allows us to create an instance of a configured ADC. The new method takes two parameters which are the ADC peripheral instance (adc1 according to the table above) and a configuration instance. As such I create an adc handle as follows:Config comes from the esp_idf_hal::adc::config module and contains configuration information like the resolution of the ADC. By default, the resolution used is 12-bits.3️⃣Configure and Obtain a handle for the ADC channel: At this point, we configured the ADC but not yet the pin/channel. This is done using the AdcChannelDriver new method. The new method takes only one argument, which is an instance of a pin. I create an adc_pin handle that represents a configured channel as follows:Note how an attenuation Atten11dB is specified in the type parameters. This is necessary to define the attenuation level of the pin and thus the voltage range the pin will be able to measure. The documentation specifies four different attenuation levels of which the 11dB level supports the range of 0 mV ~ 2500 mV which is what we're going to use.This is it for configuration! Let's now jump into the application code.Following the design described earlier, before entering my loop, I first need to set up a couple of constants that I will be using in my calculations. This includes keying in the constant values for \( \beta \) and VMAX as follows:After entering the program loop, as the software design stated earlier, first thing I need to do is kick off the ADC to obtain a sample/reading. This is done through the read method that takes a mutable reference to the adc_pin instance and returns a Result:Note here that the read method in the esp-idf-hal, unlike the read method in the no_std libraries returns a voltage, not a raw value. Next, I convert the sample value to a temperature by implementing the earlier derived equations as follows:A few things to note here; First, recall from the read method that sample is a u16, so I had to use as f64 to cast it as an f64 for the calculation. Second, log is the natural logarithm obtained from the libm library that I imported earlier. Third, and last, the temperature is calculated in Kelvins, the 273.15 is what converts it to Celcius. Like mentioned earlier the Beta parameter is determined based on Kelvins.Finally, now that the temperature is available, I send it over to the console using the println! macro as follows:This is it!Here is the full code for the implementation described in this post. You can additionally find the full project and others available on the apollolabs ESP32C3 git repo. Also, the Wokwi project can be accessed here.In this post, an analog temperature measurement application was created by leveraging the esp-idf-hal ADC peripheral for the ESP32C3. The resulting measurement is also sent over to terminal output. Additionally, all code was created at the HAL level using the esp32c3-hal. Have any questions? Share your thoughts in the comments below 👇.If you find this post useful, and to keep up to date with similar posts, here's the list of channels you can follow/subscribe to:Follow Me on TwitterSubscribe to NewsletterFollow me on GitHubClick Here for Rust Swag Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Julien - Aug 7 Sriparno Roy - Aug 9 ashish - Aug 9 Muhammad Harith Zainudin - Aug 7 Once suspended, apollolabsbin will not be able to comment or publish posts until their suspension is removed. Once unsuspended, apollolabsbin will be able to comment and publish posts again. Once unpublished, all posts by apollolabsbin will become hidden and only accessible to themselves. If apollolabsbin is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Omar.unwrap();. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag apollolabsbin: apollolabsbin consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging apollolabsbin will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

ESP32 Standard Library Embedded Rust: Analog Temperature Sensing using the ADC

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×