Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot-open:practical:hardware:sut:esp32:emb2_1 [2024/03/22 10:14] – [Start] pczekalskien:iot-open:practical:hardware:sut:esp32:emb2_1 [2025/04/28 20:31] (current) – [Steps] pczekalski
Line 1: Line 1:
-<todo @pczekalski>DOkończyć</todo> 
 ====== 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, there are no movable parts.\\ Digital potentiometer DS1803 is an I2C-controlled device that can digitally control the potentiometer.\\ Opposite to the physical potentiometers, there are no movable parts.\\
Line 26: Line 25:
 #include <Wire.h> #include <Wire.h>
 </code> </code>
-<note important>Also, remember to add the display handling library, as present in the scenarios. We suggest using OLED or ePaper to present the relation between the setting and reading as a graphic or even more than one (e.g. LCD + OLED).</note>+<note important>Also, remember to add the display handling library, as present in the scenarios. We suggest using OLED or ePaper to present the relation between the setting and reading as a graphic or even more than one display (e.g. LCD + OLED) to handle readings and the graph.</note>
  
 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           //GPIO 7 in ESP32 
-#define DS1803_ADDRESS 0x28 //I2C Address 
  
 enum POT_LIST {POT_1 = 0xA9, POT_2=0xAA, POT_ALL=0xAF}; //We have only POT_1 connected enum POT_LIST {POT_1 = 0xA9, POT_2=0xAA, POT_ALL=0xAF}; //We have only POT_1 connected
Line 59: Line 56:
 </code> </code>
  
-<note important>In the library above, ''readPotentiometer(...)'' function does not read the voltage over ADCIt returns a set value (the same as provided by ''setPotentiometer(...)''), which is on the digital side of the DS1803 device. Analogue input (a resulting voltage converted to the digital representation) can be obtained simply using regular ''analogRead(pin)'' that reads from the ADC.</note>+<note important>In the library above, the ''readPotentiometer(...)'' function returns a value previously set to the digital potentiometer, not an actual ADC voltage reading! It returns a set value by ''setPotentiometer(...)'', which is on the "digitalside of the DS1803 device. Actual ADC reading can be obtained using ''analogRead(pin)''.</note>
 ===== Suggested Readings and Knowledge Resources ===== ===== Suggested Readings and Knowledge Resources =====
   * [[en:iot-open:introductiontoembeddedprogramming2:cppfundamentals]]   * [[en:iot-open:introductiontoembeddedprogramming2:cppfundamentals]]
Line 73: Line 70:
  
 ==== Start ==== ==== Start ====
-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.+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 ====
-// Write some extra information ifi.e. some steps are optional; otherwise, cancel this paragraph (but do not remove the header).//+Belowwe assume that you have embedded functions handling operations on the digital potentiometer as defined above in your source fileRemember to add ''Wire.h'' include! 
 +<note tip>Note: Step 5 presents some stub code for displaying data on an OLED display.</note>
 === Step 1 === === Step 1 ===
-//Describe activities done in 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 
 +</code> 
 +=== 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];  
 +</code> 
 +=== 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,SCL); 
 +  delay(100); 
 +  ... 
 +   
 +  pinMode(POT_ADC, INPUT); 
 +</code>
  
-...+<note important>You need to initialise the I2C bus only onceIf you're using other I2C devices in parallel, do not call ''Wire.begin(...)'' multiple times.</note>
  
-=== Step === +=== Step === 
-//Describe activities done in Step n.//+Read the loopback characteristics of the digital potentiometer to the ADC loop and store it in the array: 
 +<code c> 
 +for(byte i=0; i<128; i++) 
 +  {  
 +    setPotentiometer(I2CPipe, 2*i, POT_1);  
 +    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& display
 +</code> 
 +More information in the scenario [[en:iot-open:practical:hardware:sut:esp32:emb7_1|]].
 +Note, ADC measures in the 12-bit mode (we assume such configuration, adapt ''factor'' if using other sampling resolution), so values stored in an ''aGraphArray'' array are between 0 and 4095. 
 +<code c>
 + float factor = 63./4095.;
 + for(byte x=0;x<128;x++)
 +  {
 +    int16_t y=63-round(((float)aGraphArray[x])*factor);
 +    display.setPixel(x,y);
 +  }
 +  display.display();
 +</code>
 ==== 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' implementation imperfection (ESP32's ADC input and digital potentiometer output) or because of the electromagnetic noise. There are many devices in our lab room.+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' implementation imperfection (ESP32's ADC input and digital potentiometer output) or because of the electromagnetic noise. There are many devices in our lab room.
  
 ===== FAQ ===== ===== FAQ =====
en/iot-open/practical/hardware/sut/esp32/emb2_1.1711102446.txt.gz · Last modified: 2024/03/22 10:14 by pczekalski
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0