Using the ADC in NodeMCU

NodeMCU has on analog channel which is configured to read the System voltage, we can configure it to sample the external pin.

The ADC seems a bit jumping around for constant voltages as well, so it is better to take average of several readings if you are doing something serious with it.

adc.force_init_mode(mode) allows to set the modes.

The modes for the ADC are

adc.INIT_ADC  for sampling external pin
adc.INIT_VDD33 gives the system voltage

adc.read(0) to sample the external pin, adc.readvdd33() to read the system voltage. You have to set the node in correct mode before you call the corresponding methods. You will get 65535 if the node is not in correct mode.

Using the ADC in NodeMCU
if adc.force_init_mode(adc.INIT_ADC)
then
 print("ADC Config changed,Restarting the node")
 node.restart()
 return -- don't bother continuing, the restart is scheduled
end

val = adc.read(0)

The following shows the result of adc.read(0) when the A0 is connected to Ground.

ADC_ON_NodeMCU

 

Tags:,

Add a Comment

Your email address will not be published. Required fields are marked *