How to read the value of analog pin A0 into a javascript variable?

I have the LDR connected to A0. I want to read the value of LDR’s output and use it in my code for further processing. In my product pin configuration, I call this pin as “light”, without the quotes

For now, I only know to use this in plotChart function. How can I assign this to a javascript variable and toggle an LED based on the value of the LDR output?

Somthing like this:
setChartTitle(‘Light Intensity’);
plotChart(‘time_stamp’, ‘light’);
// Copy the analog reading into a variable
light_variable = light // This needs to be modified
if (light_variable > 512) {
singleButton({name:“Led On”, action:“digitalWrite”,
pin:“0”, value:“HIGH”,bgcolor:“green”,
shape:“rectangle”,align:“left”,“text_color”:“white” });
} else {
singleButton({name:“Led Off”, action:“digitalWrite”,
pin:“0”, value:“LOW”, bgcolor:“red”,
shape:“rectangle”, align:“left”, text_color:“black”});

1 Like


Can you check this?

you can use this as it helped me with the light buzzer system.
it takes analog input and saves the value to the
light_variable = analogRead(‘A0’);
please tell me if it doesn’t work.

Thanks @shubham11gupta10 that worked, however I hit another issue.
The code runs only once, should I setup a timer event to read analog data? Or is there any other event that I can hook my code onto?

Buddy mark my statement as solution
And fir this you can try a timer … I am looking for some other way to breach this thing… Better create a new query and tag me their… I am on it buddy