This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Is my ADC source code correct?

Hi,

I am using ADC Channel AIN4 for reading Grove Moisture sensor input. Please let me know whether I have done the coding correctly.

#define MOISTURE_SENSOR_ADC_CHANNEL_NUM    HAL_ADC_CHN_AIN4

void MoistureSensorSetup(void)
{
//Configure the digital port pin for reading the sensor data
P0SEL |= (0x01 << MOISTURE_SENSOR_ADC_CHANNEL_NUM);
P0INP &= ~(0x01 << MOISTURE_SENSOR_ADC_CHANNEL_NUM);
P0DIR &= ~(0x01 << MOISTURE_SENSOR_ADC_CHANNEL_NUM);
APCFG |= (0x01 << MOISTURE_SENSOR_ADC_CHANNEL_NUM);

AdcChannelNumber = MOISTURE_SENSOR_ADC_CHANNEL_NUM;

HalAdcSetReference(HAL_ADC_REF_AVDD);
}

uint16 ReadMoistureSensorAnalogInput(void)
{
uint16 moistureValue; //Variable to store the value coming from the sensor

moistureValue = HalAdcRead(AdcChannelNumber, HAL_ADC_RESOLUTION_10);

return moistureValue;
}

AVDD5 is connected to 3.3V.

I am seeing only o voltage on the sensor input pin. I provided 4.7V to Grove Moisture Sensor.

Please let me know my source code is correct or any other changes I have to do.

Thanks

  • You only need the following code to read ADC value from P0_4

    uint16 adc_ain4=0;
    HalAdcSetReference(HAL_ADC_REF_AVDD);
    adc_ain4=HalAdcRead(HAL_ADC_CHN_AIN4,HAL_ADC_RESOLUTION_10);

    Since your AVDD is connected to 3,3V, the max measurable value should be no greater than 3.3V.