Skip to content

Commit

Permalink
Fix: ADCs provide erroneous readings. (#3)
Browse files Browse the repository at this point in the history
This is due to the fact that the same pins can be used both as analog
and digital inputs. The problem however is, that once pins are configured
as digital input pins they no longer report correct values when used as
analog inputs.

By their nature (as defined via the schematic) those pins are primarily
defined as analog inputs and are consequently used as such. A digital
input functionality is emulated on top of the analog one, by putting
the transfer point between LOW and HIGH at the halfway point of the
full analog voltage range.
  • Loading branch information
aentinger authored Jun 13, 2024
1 parent 668282c commit 641af99
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions examples/opcua_server/opcua_server.ino
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,24 @@ static float arduino_opta_analog_read(pin_size_t const pin)
static float const RESOLUTION = 4096.0; // 12-bit resolution
static float const DIVIDER = 0.3034; // Voltage divider

/* Read the actual analog value from the pin. */
int const pin_value = analogRead(pin);
/* Convert the raw ADC value into an actual voltage. */
float const pin_voltage = pin_value * (VOLTAGE_MAX / RESOLUTION) / DIVIDER;

return pin_voltage;
}

static PinStatus arduino_opta_digital_read(pin_size_t const pin)
{
float const pin_voltage = arduino_opta_analog_read(pin);

if (pin_voltage > 5.f) /* Half of the full range as measurable by the ADC. */
return HIGH;
else
return LOW;
}

/**************************************************************************************
* SETUP/LOOP
**************************************************************************************/
Expand Down Expand Up @@ -205,15 +217,20 @@ void setup()
}
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Arduino Opta Variant: %s", opcua::ArduinoOptaVariant::toString(opta_type).c_str());

/* Read all analog inputs at least once to have them pre-configured as ADCs. */
std::list<pin_size_t> const ADC_PIN_LIST = { A0, A1, A2, A3, A4, A5, A6, A7 };
for (auto const adc_pin : ADC_PIN_LIST)
arduino_opta_analog_read(adc_pin);
/* Configure analog solution to 12-Bit. */
analogReadResolution(12);

/* Define the Arduino Opta as a OPC/UA object. */
arduino_opta_opcua = opcua::ArduinoOpta::create(opc_ua_server, opta_type);
if (!arduino_opta_opcua) {
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "opcua::ArduinoOpta::create(...) failed");
return;
}

/* Configure analog solution to 12-Bit. */
analogReadResolution(12);
/* Add the various digital input pins. */
arduino_opta_opcua->analog_input_mgr()->add_analog_input(opc_ua_server, "Analog Input 1", []() { return arduino_opta_analog_read(A0); });
arduino_opta_opcua->analog_input_mgr()->add_analog_input(opc_ua_server, "Analog Input 2", []() { return arduino_opta_analog_read(A1); });
Expand All @@ -225,14 +242,14 @@ void setup()
arduino_opta_opcua->analog_input_mgr()->add_analog_input(opc_ua_server, "Analog Input 8", []() { return arduino_opta_analog_read(A7); });

/* Add the various digital input pins. */
arduino_opta_opcua->digital_input_mgr()->add_digital_input(opc_ua_server, "Digital Input 1", []() { pinMode(A0, INPUT); return digitalRead(A0); });
arduino_opta_opcua->digital_input_mgr()->add_digital_input(opc_ua_server, "Digital Input 2", []() { pinMode(A1, INPUT); return digitalRead(A1); });
arduino_opta_opcua->digital_input_mgr()->add_digital_input(opc_ua_server, "Digital Input 3", []() { pinMode(A2, INPUT); return digitalRead(A2); });
arduino_opta_opcua->digital_input_mgr()->add_digital_input(opc_ua_server, "Digital Input 4", []() { pinMode(A3, INPUT); return digitalRead(A3); });
arduino_opta_opcua->digital_input_mgr()->add_digital_input(opc_ua_server, "Digital Input 5", []() { pinMode(A4, INPUT); return digitalRead(A4); });
arduino_opta_opcua->digital_input_mgr()->add_digital_input(opc_ua_server, "Digital Input 6", []() { pinMode(A5, INPUT); return digitalRead(A5); });
arduino_opta_opcua->digital_input_mgr()->add_digital_input(opc_ua_server, "Digital Input 7", []() { pinMode(A6, INPUT); return digitalRead(A6); });
arduino_opta_opcua->digital_input_mgr()->add_digital_input(opc_ua_server, "Digital Input 8", []() { pinMode(A7, INPUT); return digitalRead(A7); });
arduino_opta_opcua->digital_input_mgr()->add_digital_input(opc_ua_server, "Digital Input 1", []() { return arduino_opta_digital_read(A0); });
arduino_opta_opcua->digital_input_mgr()->add_digital_input(opc_ua_server, "Digital Input 2", []() { return arduino_opta_digital_read(A1); });
arduino_opta_opcua->digital_input_mgr()->add_digital_input(opc_ua_server, "Digital Input 3", []() { return arduino_opta_digital_read(A2); });
arduino_opta_opcua->digital_input_mgr()->add_digital_input(opc_ua_server, "Digital Input 4", []() { return arduino_opta_digital_read(A3); });
arduino_opta_opcua->digital_input_mgr()->add_digital_input(opc_ua_server, "Digital Input 5", []() { return arduino_opta_digital_read(A4); });
arduino_opta_opcua->digital_input_mgr()->add_digital_input(opc_ua_server, "Digital Input 6", []() { return arduino_opta_digital_read(A5); });
arduino_opta_opcua->digital_input_mgr()->add_digital_input(opc_ua_server, "Digital Input 7", []() { return arduino_opta_digital_read(A6); });
arduino_opta_opcua->digital_input_mgr()->add_digital_input(opc_ua_server, "Digital Input 8", []() { return arduino_opta_digital_read(A7); });

/* Add the various relay outputs. */
arduino_opta_opcua->relay_mgr()->add_relay_output(opc_ua_server, "Relay 1", [](bool const value) { pinMode(RELAY1, OUTPUT); digitalWrite(RELAY1, value); pinMode(LED_D0, OUTPUT); digitalWrite(LED_D0, value); });
Expand Down

0 comments on commit 641af99

Please sign in to comment.