This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot-open:practical:hardware:sut:esp32:emb2_1 [2024/03/24 21:37] – [Steps] pczekalski | en:iot-open:practical:hardware:sut:esp32:emb2_1 [2025/04/28 20:31] (current) – [Steps] pczekalski | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | <todo @pczekalski> | ||
====== EMB2: Using a digital potentiometer ===== | ====== EMB2: Using a digital potentiometer ===== | ||
Digital potentiometer DS1803 is an I2C-controlled device that can digitally control the potentiometer.\\ Opposite to the physical potentiometers, | Digital potentiometer DS1803 is an I2C-controlled device that can digitally control the potentiometer.\\ Opposite to the physical potentiometers, | ||
Line 26: | Line 25: | ||
#include < | #include < | ||
</ | </ | ||
- | <note important> | + | <note important> |
Below, we present a sample control library that you need to include in your code: | Below, we present a sample control library that you need to include in your code: | ||
<code c> | <code c> | ||
- | #define POT_ADC 7 // | ||
- | #define DS1803_ADDRESS 0x28 //I2C Address | ||
enum POT_LIST {POT_1 = 0xA9, POT_2=0xAA, POT_ALL=0xAF}; | enum POT_LIST {POT_1 = 0xA9, POT_2=0xAA, POT_ALL=0xAF}; | ||
Line 59: | Line 56: | ||
</ | </ | ||
- | <note important> | + | <note important> |
===== Suggested Readings and Knowledge Resources ===== | ===== Suggested Readings and Knowledge Resources ===== | ||
* [[en: | * [[en: | ||
Line 75: | Line 72: | ||
Check if you can see all the displays. Remember to use potentiometer 1 (index 0) because only this one is connected to the ADC input of the ESP32 MCU. In these steps, we present only how to handle communication with a digital potentiometer and how to read the ADC input of the MCU. Methods for displaying the measurements and plotting the graph are present in other scenarios. Remember to include the functions above in your code unless you want to integrate them with your solution. | Check if you can see all the displays. Remember to use potentiometer 1 (index 0) because only this one is connected to the ADC input of the ESP32 MCU. In these steps, we present only how to handle communication with a digital potentiometer and how to read the ADC input of the MCU. Methods for displaying the measurements and plotting the graph are present in other scenarios. Remember to include the functions above in your code unless you want to integrate them with your solution. | ||
==== Steps ==== | ==== Steps ==== | ||
- | Below, we assume that you have embedded functions handling operations on the digital potentiometer as defined above in your source file. | + | Below, we assume that you have embedded functions handling operations on the digital potentiometer as defined above in your source file. Remember to add '' |
+ | <note tip> | ||
=== Step 1 === | === Step 1 === | ||
+ | Define I2C bus GPIOs: clock (SCL) uses GPIO 4, and data (SDA) uses GPIO 5. ADC uses GPIO 7. Digital potentiometer chip DS1803 uses 0x28 I2C address. All definitions are present in the following code: | ||
+ | <code c> | ||
+ | #define SCL 4 | ||
+ | #define SDA 5 | ||
+ | #define POT_ADC 7 | ||
+ | #define DS1803_ADDRESS 0x28 | ||
+ | </ | ||
+ | === Step 2 === | ||
+ | Declare an array of readings that fits an OLED display. Adjust for ePaper resolution (horizontal) if using it. OLED is 128x128 pixels: | ||
+ | <code c> | ||
+ | static int16_t aGraphArray[128]; | ||
+ | </ | ||
+ | === Step 3 === | ||
+ | Include functions present in the PREREQUISITES section. | ||
+ | === Step 4 === | ||
+ | Initialise the I2C bus and configure ADC's GPIO as input: | ||
+ | <code c> | ||
+ | Wire.begin(SDA, | ||
+ | delay(100); | ||
+ | ... | ||
+ | | ||
+ | pinMode(POT_ADC, | ||
+ | </ | ||
+ | <note important> | ||
- | ... | + | === Step 4 === |
- | + | Read the loopback characteristics of the digital potentiometer to the ADC loop and store it in the array: | |
- | === Step n === | + | <code c> |
- | //Describe activities done in Step n.// | + | for(byte i=0; i<128; i++) |
+ | { | ||
+ | setPotentiometer(I2CPipe, | ||
+ | aGraphArray[i]=analogRead(POT_ADC); | ||
+ | } | ||
+ | </code> | ||
+ | === Step 5 === | ||
+ | Display on the OLED. Assume the following handler to the pointer to the display controller class: | ||
+ | <code c> | ||
+ | SSD1306Wire& | ||
+ | </ | ||
+ | More information in the scenario [[en: | ||
+ | Note, ADC measures in the 12-bit mode (we assume such configuration, | ||
+ | <code c> | ||
+ | float factor = 63./4095.; | ||
+ | | ||
+ | { | ||
+ | int16_t y=63-round(((float)aGraphArray[x])*factor); | ||
+ | display.setPixel(x, | ||
+ | } | ||
+ | display.display(); | ||
+ | </ | ||
==== Result validation ==== | ==== Result validation ==== | ||
- | A relation between the potentiometer set value and ADC reading should be almost linear from 0V up to about 3V. It becomes horizontal because the ESP32 chip limits the ADC range to 3V, so going beyond 3V (and due to the electronic construction as in figure {{ref>xxx}} it may go to about 3.3V) gives no further increase but rather a reading of the 4096 value (which means the input voltage is over the limit). For this reason, your plot may be finished suddenly with a horizontal instead of linearity decreasing function. It is by design. ADC input of the ESP32 can tolerate values between 3V and 3.3V. The linear correlation mentioned above is never perfect, either because of the devices' | + | A relation between the potentiometer set value and ADC reading should be almost linear from 0V up to about 3V. It becomes horizontal because the ESP32 chip limits the ADC range to 3V, so going beyond 3V (and due to the electronic construction as in figure {{ref>figuredigipot}} it may go to about 3.3V) gives no further increase but rather a reading of the 4096 value (which means the input voltage is over the limit). For this reason, your plot may be finished suddenly with a horizontal instead of linearity decreasing function. It is by design. ADC input of the ESP32 can tolerate values between 3V and 3.3V. The linear correlation mentioned above is never perfect, either because of the devices' |
===== FAQ ===== | ===== FAQ ===== |