Skip to content

Commit c29e9e8

Browse files
committed
current sensor 2.0
1 parent 0c62e75 commit c29e9e8

File tree

1 file changed

+145
-1
lines changed
  • content/hardware/07.opta/opta-family/opta/tutorials/01.user-manual

1 file changed

+145
-1
lines changed

content/hardware/07.opta/opta-family/opta/tutorials/01.user-manual/content.md

Lines changed: 145 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3026,6 +3026,12 @@ AnalogExpansion::beginChannelAsAdc(OptaController, // the expansion object
30263026
false, // disable diagnostic
30273027
0); // disable averaging
30283028
```
3029+
3030+
You can also use the simplified dedicated method using the function `beginChannelAsVoltageAdc` as follows:
3031+
3032+
```arduino
3033+
exp.beginChannelAsVoltageAdc(<exp channel>); // pass the desired input as argument
3034+
```
30293035
The function `optaAnalogTask()` reads all the analog input raw ADC values and prints out them. If you want to show the voltage reading instead use the following function:
30303036

30313037
```arduino
@@ -3220,6 +3226,13 @@ AnalogExpansion::beginChannelAsAdc(OptaController, // the expansion object
32203226
false, // disable diagnostic
32213227
0); // disable averaging
32223228
```
3229+
3230+
You can also use the simplified dedicated method using the function `beginChannelAsCurrentAdc` as follows:
3231+
3232+
```arduino
3233+
exp.beginChannelAsCurrentAdc(<exp channel>); // pass the desired input as argument
3234+
```
3235+
32233236
The function `optaAnalogTask()` reads all the analog input current values and prints out them.
32243237

32253238
After the Opta™ controller is programmed with the example sketch, open the Arduino IDE Serial Monitor and you will see each input reading as follows:
@@ -3237,11 +3250,142 @@ Analog Expansion n. 0
32373250
```
32383251
![Analog current input wiring example](assets/analog-4-20-inputs.png)
32393252

3240-
There is another approach for interfacing 4-20 mA sensors that consists of defining the channel as a voltage output, connecting the sensor to the channel and measuring the current of the loop. Use the following example sketch instead:
3253+
There is another approach for interfacing 4-20 mA sensors that consists of defining the channel as a **voltage DAC** and adding a **current ADC** to the same channel, connecting the sensor to the channel and measuring the current of the loop. Use the following example sketch instead:
3254+
3255+
```arduino
3256+
#include "OptaBlue.h"
3257+
3258+
#define PERIODIC_UPDATE_TIME 2000
3259+
#define DELAY_AFTER_SETUP 200
3260+
3261+
#define SENSOR_CH 0
3262+
3263+
/* -------------------------------------------------------------------------- */
3264+
void printExpansionType(ExpansionType_t t) {
3265+
/* -------------------------------------------------------------------------- */
3266+
if (t == EXPANSION_NOT_VALID) {
3267+
Serial.print("Unknown!");
3268+
} else if (t == EXPANSION_OPTA_DIGITAL_MEC) {
3269+
Serial.print("Opta --- DIGITAL [Mechanical] ---");
3270+
} else if (t == EXPANSION_OPTA_DIGITAL_STS) {
3271+
Serial.print("Opta --- DIGITAL [Solid State] ---");
3272+
} else if (t == EXPANSION_DIGITAL_INVALID) {
3273+
Serial.print("Opta --- DIGITAL [!!Invalid!!] ---");
3274+
} else if (t == EXPANSION_OPTA_ANALOG) {
3275+
Serial.print("~~~ Opta ANALOG ~~~");
3276+
} else {
3277+
Serial.print("Unknown!");
3278+
}
3279+
}
3280+
3281+
/* -------------------------------------------------------------------------- */
3282+
void printExpansionInfo() {
3283+
/* -------------------------------------------------------------------------- */
3284+
static long int start = millis();
3285+
3286+
if (millis() - start > 5000) {
3287+
start = millis();
3288+
Serial.print("Number of expansions: ");
3289+
Serial.println(OptaController.getExpansionNum());
3290+
3291+
for (int i = 0; i < OptaController.getExpansionNum(); i++) {
3292+
Serial.print("Expansion n. ");
3293+
Serial.print(i);
3294+
Serial.print(" type ");
3295+
printExpansionType(OptaController.getExpansionType(i));
3296+
Serial.print(" I2C address ");
3297+
Serial.println(OptaController.getExpansionI2Caddress(i));
3298+
}
3299+
}
3300+
}
3301+
3302+
int8_t oa_index = -1;
3303+
/* -------------------------------------------------------------------------- */
3304+
/* SETUP */
3305+
/* -------------------------------------------------------------------------- */
3306+
void setup() {
3307+
/* -------------------------------------------------------------------------- */
3308+
Serial.begin(115200);
3309+
delay(2000);
3310+
3311+
OptaController.begin();
3312+
3313+
for (int i = 0; i < OptaController.getExpansionNum(); i++) {
3314+
AnalogExpansion exp = OptaController.getExpansion(i);
3315+
3316+
if (exp) {
3317+
// start the channel as a voltage DAC
3318+
exp.beginChannelAsDac(SENSOR_CH, //channel index
3319+
OA_VOLTAGE_DAC, //DAC type
3320+
false, //limit current (set to false so it can power the sensor current loop)
3321+
false, //No slew rate
3322+
OA_SLEW_RATE_0); //Slew rate setting.
3323+
3324+
Serial.println("Setting DAC output to 11 V on expansion n. " + String(exp.getIndex()));
3325+
exp.pinVoltage(SENSOR_CH, 11.0, true); // set channel 0 output to 11 V (Max voltage output)
3326+
delay(200); // give time for the channel to be set up
3327+
// add a current ADC to the same channel
3328+
exp.addCurrentAdcOnChannel(SENSOR_CH);
3329+
3330+
}
3331+
}
3332+
}
3333+
3334+
3335+
/* -------------------------------------------------------------------------- */
3336+
void optaAnalogTask() {
3337+
/* -------------------------------------------------------------------------- */
3338+
3339+
static long int start = millis();
3340+
3341+
/* using this the code inside the if will run every PERIODIC_UPDATE_TIME ms
3342+
assuming the function is called repeteadly in the loop() function */
3343+
3344+
if (millis() - start > PERIODIC_UPDATE_TIME) {
3345+
start = millis();
3346+
3347+
3348+
for (int i = 0; i < OptaController.getExpansionNum(); i++) {
3349+
AnalogExpansion exp = OptaController.getExpansion(i);
3350+
if (exp) {
3351+
float value = exp.pinCurrent(SENSOR_CH);
3352+
Serial.println("- ch" + String(SENSOR_CH) + " -> Current " + String(abs(value)) + " mA");
3353+
}
3354+
}
3355+
}
3356+
}
3357+
3358+
/* -------------------------------------------------------------------------- */
3359+
/* LOOP */
3360+
/* -------------------------------------------------------------------------- */
3361+
void loop() {
3362+
/* -------------------------------------------------------------------------- */
3363+
OptaController.update();
3364+
//printExpansionInfo();
3365+
optaAnalogTask();
3366+
}
3367+
3368+
```
3369+
The key section of the example from above is in the `setup()` function, specifically in the way we initialize the channel to be used for the measurement:
32413370

32423371
```arduino
3372+
// start the channel as a voltage DAC
3373+
exp.beginChannelAsDac(SENSOR_CH, //channel index
3374+
OA_VOLTAGE_DAC, //DAC type
3375+
false, //limit current (set to false so it can power the sensor current loop)
3376+
false, //No slew rate
3377+
OA_SLEW_RATE_0); //Slew rate setting.
3378+
3379+
Serial.println("Setting DAC output to 11 V on expansion n. " + String(exp.getIndex()));
3380+
exp.pinVoltage(SENSOR_CH, 11.0, true); // set channel 0 output to 11 V (Max voltage output)
3381+
delay(200); // give time for the channel to be set up
3382+
// add a current ADC to the same channel
3383+
exp.addCurrentAdcOnChannel(SENSOR_CH);
32433384
```
32443385

3386+
First, the channel is initialized as a voltage DAC with the "limit current" parameter disabled, a voltage is set in the output that will power the current loop and then a current ADC is added to the same channel, this way we can use the `pinCurrent()` function to measure the current output of the sensor.
3387+
3388+
32453389

32463390
#### Analog RTD Input Mode
32473391

0 commit comments

Comments
 (0)