Curriculum
Course: LDR Sensor with Led
Login
Text lesson

MBlock Coding

Step 1 — Create Variable

First create a variable named:

value
 

This variable stores the LDR sensor reading.


Step 2 — Main Program Blocks

Full Logic

When Arduino Uno starts up
forever
 

Step 3 — Read Sensor Value

Inside forever block add:

 
set value to analog read pin A0
 

Explanation

  • LDR sensor is connected to A0
  • Arduino continuously reads light value
  • The reading is stored in variable value
  •  

Step 4 — Add Condition

After reading value add:

 
if value > 500 then
 

Explanation

  • If light is bright,
  • sensor value becomes greater than 500

So condition becomes TRUE.


Step 5 — LED ON

Inside IF block add:

 
set digital pin 8 output as HIGH
 

Explanation

  • HIGH means ON
  • LED connected to pin 8 glows

Step 6 — Else Condition

Add ELSE section:

 
else
set digital pin 8 output as LOW
 

Explanation

  • LOW means OFF
  • When light becomes low,
  • LED turns OFF

Final MBlock Logic

 
When Arduino Uno starts up

forever

set value to analog read pin A0

if value > 500 then

set digital pin 8 output as HIGH

else

set digital pin 8 output as LOW
×
×

Cart