The supported Analog Sensor Output Types are:
- 4-20mA
- 0-10V.
This example will supply power to an Analog sensor, read the current (in uA ) OR voltage (in mV) level at the EXT_ANALOG_IN
pin and print the value on the debug console.
#include <stdio.h>
#include "flex.h"
#define APPLICATION_NAME "Analog Example"
#define MEASURE_CURRENT 1
#if MEASURE_CURRENT
#define ANALOG_IN_MODE FLEX_ANALOG_IN_CURRENT
#define APPLICATION_MODE "current"
#else
#define ANALOG_IN_MODE FLEX_ANALOG_IN_VOLTAGE
#define APPLICATION_MODE "voltage"
#endif
#define SENSOR_READINGS_PER_DAY 4
#define ANALOG_SENSOR_POWER_IN FLEX_POWER_OUT_24V
#define DELAY_SENSOR_STABILISE_MS 1500
static uint32_t MeasureAnalogInput(void) {
uint32_t SensorReading = UINT32_MAX;
printf("Failed to Init Power Out.\r\n");
goto fail_0;
}
printf("Failed to Init Analog Input.\r\n");
goto fail_1;
}
#if MEASURE_CURRENT
printf("Failed to Read Current.\r\n");
}
#else
printf("Failed to Read Voltage.\r\n");
}
#endif
fail_1:
fail_0:
return SensorReading;
}
static time_t PrintSensorReading(void) {
uint32_t analog_sensor_reading = MeasureAnalogInput();
if (analog_sensor_reading != UINT32_MAX) {
#if MEASURE_CURRENT
printf("Current = %luuA.\r\n", analog_sensor_reading);
#else
printf("Voltage = %lumV.\r\n", analog_sensor_reading);
#endif
}
return (
FLEX_TimeGet() + 24 * 3600 / SENSOR_READINGS_PER_DAY);
}
void FLEX_AppInit() {
printf("%s (%s mode).\r\n", APPLICATION_NAME, APPLICATION_MODE);
}