Hello Nordic team,
For the (POC), I configured ADC channel 1 and interfaced a potentiometer to pin P1.14. The ADC was set up accordingly to read raw value from the potentiometer./ {
zephyr,user {
io-channels = <&adc 1>;
};
};
&adc {
#address-cells = <1>;
#size-cells = <0>;
channel@1 {
reg = <1>;
zephyr,gain = "ADC_GAIN_1";
zephyr,reference = "ADC_REF_INTERNAL";
zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
zephyr,input-positive = <NRF_SAADC_AIN7>; // P1.14
zephyr,resolution = <12>;
zephyr,oversampling = <2>; // Enable oversampling with a factor of 2
};
};
As per "Exercise 1 – Interfacing with ADC using Zephyr API", we configured and initialized the ADC, and invoked it within the main()
function.
We also included a function call to read the ADC value, as shown in the following code snippet://adc.cpp
int32_t readValue(void)
{
int err;
err = adc_read_dt(&smoke_adc, &smoke_seq);
if (err < 0) {
LOG_ERR("Smoke ADC read failed (%d)", err);
return err;
}
LOG_INF("Smoke rawvalue : %d\n",smoke_buf);
}
To analyze the ADC reading values, we observed two scenarios:
1) As mentioned in the code below, when we called the readValue()
function within the while(1)
loop in main.cpp
.//main.cpp
int_main()
{
if(smoke_init())
{
LOG_ERR("Smoke init failed");
}
while(1)
{
readValue();
k_msleep(1000);
}
commenting StartApp function
CHIP_ERROR err = AppTask::Instance().StartApp();
LOG_ERR("Exited with code %" CHIP_ERROR_FORMAT, err.Format());
return err == CHIP_NO_ERROR ? EXIT_SUCCESS : EXIT_FAILURE;
}
2) The readVoltage()
function is called every second using a timer, which is initialized through the SensorTimerHandler
in the Apptask:Init()
function. This function also initializes the Matter stack.
CHIP_ERROR AppTask::Init() { /* Initialize Matter stack */ ReturnErrorOnFailure(Nrf::Matter::PrepareServer()); if (!Nrf::GetBoard().Init(ButtonEventHandler)) { LOG_ERR("User interface initialization failed."); return CHIP_ERROR_INCORRECT_STATE; } k_timer_init(&mSelfTestLedTimer, &SelfTestLedTimerTimeoutCallback, nullptr); k_timer_init(&mSelfTestTimer, &SelfTestTimerTimeoutCallback, nullptr); /* Register Matter event handler that controls the connectivity status LED based on the captured Matter network * state. */ ReturnErrorOnFailure(Nrf::Matter::RegisterEventHandler(Nrf::Board::DefaultMatterEventHandler, 0)); /* Register Smoke CO Alarm test event triggers */ #ifdef CONFIG_NCS_SAMPLE_MATTER_TEST_EVENT_TRIGGERS /* Register Smoke CO alarm test events handler */ static chip::SmokeCOTestEventTriggerHandler sSmokeCoAlarmTestEventTrigger; ReturnErrorOnFailure(Nrf::Matter::TestEventTrigger::Instance().RegisterTestEventTriggerHandler( &sSmokeCoAlarmTestEventTrigger)); /* Register test event handler to change the power source. */ ReturnErrorOnFailure(Nrf::Matter::TestEventTrigger::Instance().RegisterTestEventTrigger( kPowerSourceOnEventTriggerId, Nrf::Matter::TestEventTrigger::EventTrigger{ 0xFFFF, PowerSourceOnEventCallback })); ReturnErrorOnFailure(Nrf::Matter::TestEventTrigger::Instance().RegisterTestEventTrigger( kPowerSourceOffEventTriggerId, Nrf::Matter::TestEventTrigger::EventTrigger{ 0xFFFF, PowerSourceOffEventCallback })); #endif // Read ADC value using Timer k_timer_init(&sSensorTimer, &SensorTimerHandler, nullptr); k_timer_user_data_set(&sSensorTimer, this); SensorActivateHandler(); return Nrf::Matter::StartServer(); } CHIP_ERROR AppTask::StartApp() { ReturnErrorOnFailure(Init()); while (true) { Nrf::DispatchNextTask(); } return CHIP_NO_ERROR; }
Based on the above two code snippets, we will examine adc raw data values based on following two scenarios:
A) Without initializing the Matter stack, where the value is read within the
while(1)
loop in main.cpp
.B) With the Matter stack initialized, where the value is read using a timer.
- Scenario A-1 readings:
- Scenario B-1 readings:
- Scenario A-2 readings:
- Scenario B-2 readings:
In above we can see the two scenarios reading,
where mentioned A/B defined scenario (as mentioned above) and 1/2 defined reading number. -
Scenario A-1: With the potentiometer set to a specific value, we observed a reading of ~1111.
-
Scenario B-1: With the same potentiometer setting, we observed a reading of 4092.
-
Scenario A-2: At another potentiometer setting, the reading was ~241.
-
Scenario B-2: With the same potentiometer setting, the reading was around 26xx.
we can see the clear difference in both scenarios.
we want to understand why we got a wrong reading when we initialize matter stack ?
Looking forward to your insights.
Thanks & Regards,
Pratik Panchal