Difference between revisions of "LDR"

From Sketching with Hardware at LMU Wiki
Jump to navigation Jump to search
Line 24: Line 24:
 
   analogVal = analogPin.read()
 
   analogVal = analogPin.read()
 
   print(analogVal)
 
   print(analogVal)
 +
  sleep(1)
 +
</syntaxhighlight>
 +
 +
 +
== Code Example Arduino Nano Connect RP2040 ==
 +
A0 is the analog input with 16 bit resolution. It reads the analog value every second and print it to the console-
 +
 +
<syntaxhighlight lang="python" line='line'>
 +
#Example usage for Arduino Nano
 +
from machine import Pin, ADC
 +
from time import sleep
 +
 +
analogPin = ADC(Pin(26))
 +
 +
while True:
 +
  analogVal16 = analogPin.read_u16()
 +
  print(analogVal16)
 
   sleep(1)
 
   sleep(1)
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 10:30, 2 June 2024

Description

An LDR is a light-dependent resistor. It changes it's resistance depending on the light falling onto the LDR.

Ldr01.PNG

How to connect it electrically

Connecting the light dependent resistor (LDR). The value you read is dependent on how bright it is.

Rldr.JPG


How to control it in MicroPython

This makes Pin 34 an analog input and set it to 12 bit. It reads the analog value every second and print it to the console.

 1 #Example usage for ESP32
 2 from machine import Pin, ADC
 3 from time import sleep
 4 
 5 analogPin = ADC(Pin(34))
 6 analogPin.atten(ADC.ATTN_11DB)
 7 
 8 while True:
 9   analogVal = analogPin.read()
10   print(analogVal)
11   sleep(1)


Code Example Arduino Nano Connect RP2040

A0 is the analog input with 16 bit resolution. It reads the analog value every second and print it to the console-

 1 #Example usage for Arduino Nano
 2 from machine import Pin, ADC
 3 from time import sleep
 4 
 5 analogPin = ADC(Pin(26))
 6 
 7 while True:
 8   analogVal16 = analogPin.read_u16()
 9   print(analogVal16)
10   sleep(1)

Related Tutorial Videos