This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:iot-open:practical:hardware:sut:stm32:emb2_1 [2024/04/21 10:12] – created ktokarz | en:iot-open:practical:hardware:sut:stm32:emb2_1 [2024/04/21 11:25] (current) – [Result validation] ktokarz | ||
---|---|---|---|
Line 13: | Line 13: | ||
Reading of the ADC is possible using the regular '' | Reading of the ADC is possible using the regular '' | ||
- | <note tip>In STM32, ADC has by default | + | <note tip>In STM32, ADC has a 12-bit resolution |
===== Prerequisites ===== | ===== Prerequisites ===== | ||
To implement this scenario, it is advised to get familiar with at least one of the following scenarios first: | To implement this scenario, it is advised to get familiar with at least one of the following scenarios first: | ||
Line 34: | Line 34: | ||
// | // | ||
- | void setPotentiometer(TwoWire& | + | void setPotentiometer(TwoWire& |
- | byte readPotentiometer(TwoWire& | + | byte readPotentiometer(TwoWire& |
// | // | ||
- | void setPotentiometer(TwoWire& | + | void setPotentiometer(TwoWire& |
| | ||
- | | + | |
- | | + | |
- | | + | |
- | | + | |
}; | }; | ||
- | byte readPotentiometer(TwoWire& | + | byte readPotentiometer(TwoWire& |
{ | { | ||
byte buffer[2]; | byte buffer[2]; | ||
- | | + | |
- | buffer[0]=I2CPipe.read(); | + | buffer[0]=I2CDev.read(); |
- | buffer[1]=I2CPipe.read(); | + | buffer[1]=I2CDev.read(); |
return (potNumber==POT_1? | return (potNumber==POT_1? | ||
}; | }; | ||
Line 59: | Line 59: | ||
===== Suggested Readings and Knowledge Resources ===== | ===== Suggested Readings and Knowledge Resources ===== | ||
* [[en: | * [[en: | ||
- | * [[en: | + | * [[en: |
- | * [[en: | + | * [[en: |
* [[en: | * [[en: | ||
* [[https:// | * [[https:// | ||
Line 66: | Line 66: | ||
==== Task to be implemented ==== | ==== Task to be implemented ==== | ||
- | Iterate over the potentiometer settings, read related voltage readings via ADC, and present them in graphical form (as a plot). As the maximum resolution is 256, you can use a plot of 256 points or any other lower value covering all ranges. Present graph (plot) on either ePaper or OLED display, and while doing the readings, you should present data in the LCD (upper row for a set value, lower for a reading of the ADC). | + | Iterate over the potentiometer settings, read related voltage readings via ADC, and present them in graphical form (as a plot). As the maximum resolution |
==== 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 | + | 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 steps 1-3, we present 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 presented |
==== Steps ==== | ==== Steps ==== | ||
- | Below, we assume that you have embedded functions handling operations on the digital potentiometer as defined above in your source file. Remember to add '' | + | Below, we assume that you have embedded functions handling operations on the digital potentiometer as defined above in your source file. Remember to include the '' |
- | <note tip> | + | <note tip> |
=== Step 1 === | === Step 1 === | ||
- | Define | + | Define ADC pin and Digital potentiometer chip DS1803 I2C address. All definitions are present in the following code: |
<code c> | <code c> | ||
- | #define SCL 4 | + | #define POT_ADC |
- | #define SDA 5 | + | |
- | #define POT_ADC | + | |
#define DS1803_ADDRESS 0x28 | #define DS1803_ADDRESS 0x28 | ||
</ | </ | ||
+ | |||
=== Step 2 === | === Step 2 === | ||
Declare an array of readings that fits an OLED display. Adjust for ePaper resolution (horizontal) if using it. OLED is 128x128 pixels: | Declare an array of readings that fits an OLED display. Adjust for ePaper resolution (horizontal) if using it. OLED is 128x128 pixels: | ||
Line 87: | Line 87: | ||
static int16_t aGraphArray[128]; | static int16_t aGraphArray[128]; | ||
</ | </ | ||
+ | |||
=== Step 3 === | === Step 3 === | ||
Include functions present in the PREREQUISITES section. | Include functions present in the PREREQUISITES section. | ||
+ | |||
=== Step 4 === | === Step 4 === | ||
- | Initialise the I2C bus and configure ADC's GPIO as input: | + | Initialise the I2C bus and configure ADC's GPIO as input. Change the ADC resolution to 12-bits. |
<code c> | <code c> | ||
- | Wire.begin(SDA,SCL); | + | Wire.begin(); |
- | delay(100); | + | |
pinMode(POT_ADC, | pinMode(POT_ADC, | ||
+ | analogReadResolution(12); | ||
</ | </ | ||
=== Step 4 === | === Step 4 === | ||
- | Read the loopback characteristics of the digital potentiometer | + | Perform |
<code c> | <code c> | ||
for(byte i=0; i<128; i++) | for(byte i=0; i<128; i++) | ||
{ | { | ||
- | setPotentiometer(I2CPipe, 2*i, POT_1); | + | setPotentiometer(Wire, 2*i, POT_1); |
aGraphArray[i]=analogRead(POT_ADC); | aGraphArray[i]=analogRead(POT_ADC); | ||
} | } | ||
</ | </ | ||
+ | < | ||
+ | We use 128 values because the OLED display' | ||
+ | </ | ||
=== Step 5 === | === Step 5 === | ||
Display on the OLED. Assume the following handler to the pointer to the display controller class: | Display on the OLED. Assume the following handler to the pointer to the display controller class: | ||
<code c> | <code c> | ||
- | SSD1306Wire& | + | Adafruit_SSD1351 oled |
</ | </ | ||
- | More information in the scenario [[en: | + | More information |
- | Note, ADC measures in the 12-bit mode (we assume such configuration, | + | Note, ADC measures in the 12-bit mode (we assume such configuration, |
<code c> | <code c> | ||
- | float factor = 63./4095.; | + | float factor = 128./4095.; |
| | ||
{ | { | ||
- | int16_t y=63-round(((float)aGraphArray[x])*factor); | + | int16_t y=128-round(((float)aGraphArray[x])*factor); |
display.setPixel(x, | display.setPixel(x, | ||
} | } | ||
Line 124: | Line 129: | ||
</ | </ | ||
==== 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 | + | A relation between the potentiometer set value and ADC reading should be almost linear from 0V up to the maximum. The linear correlation is never perfect, either because of the devices' |
===== FAQ ===== | ===== FAQ ===== |