Skip to content

Commit 9b4b2fe

Browse files
committed
feat(wokwi-photoresistor-sensor): test with Arduino Uno
1 parent 2024dbc commit 9b4b2fe

File tree

7 files changed

+101
-0
lines changed

7 files changed

+101
-0
lines changed

.github/workflows/wokwi-test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ jobs:
4646
- name: microsd-card-esp32
4747
path: wokwi-microsd-card/sd-esp32
4848
scenario: sd.test.yaml
49+
- name: photoresistor-uno
50+
path: wokwi-photoresistor-sensor/photoresistor-uno
51+
scenario: photoresistor.test.yaml
4952
- name: slide-potentiometer-uno
5053
path: wokwi-slide-potentiometer/pot-uno
5154
scenario: slide-potentiometer.test.yaml

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ This repository contains a set of test projects for many Wokwi parts. The tests
2020
- [ESP32-C3 + I2C](./wokwi-lcd1602/lcd-i2c-esp32-c3/)
2121
- wokwi-micro-sd
2222
- [ESP32](./wokwi-micro-sd/sd-esp32/)
23+
- wokwi-photoresistor-sensor
24+
- [Arduino Uno](./wokwi-photoresistor-sensor/photoresistor-uno/)
2325
- wokwi-slide-potentiometer
2426
- [Arduino Uno](./wokwi-slide-potentiometer/pot-uno/)
2527
- [ESP32](./wokwi-slide-potentiometer/pot-esp32/)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"version": 1,
3+
"author": "Uri Shaked",
4+
"editor": "wokwi",
5+
"parts": [
6+
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 0, "left": 0, "attrs": {} },
7+
{
8+
"type": "wokwi-photoresistor-sensor",
9+
"id": "photoresistor",
10+
"top": 57.3,
11+
"left": 263.3,
12+
"rotate": 90,
13+
"attrs": { "lux": "500" }
14+
}
15+
],
16+
"connections": [
17+
[ "uno:5V", "photoresistor:VCC", "red", [ "v48.5", "h204.8", "v-67.2" ] ],
18+
[ "uno:A0", "photoresistor:AO", "green", [ "h-14.21", "v29.3", "h142.21", "v-48" ] ],
19+
[ "photoresistor:GND", "uno:GND.2", "black", [ "h0.4", "v57.6", "h-185.7" ] ]
20+
],
21+
"dependencies": {}
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: 'Photoresistor Sensor Test (ESP32)'
2+
version: 1
3+
author: 'Wokwi'
4+
5+
steps:
6+
- wait-serial: 'Photoresistor Sensor Test'
7+
# Wait for first reading
8+
- delay: 500ms
9+
# Check initial light value (should be around 500 lux)
10+
- wait-serial: 'Sensor value: 250'
11+
# Wait for second reading to confirm consistency
12+
- delay: 500ms
13+
- wait-serial: 'Sensor value: 250'
14+
# Set new lux value (bright light)
15+
- set-control:
16+
part-id: photoresistor
17+
control: lux
18+
value: 1000
19+
- delay: 500ms
20+
# Check new values (analog value should be lower)
21+
- wait-serial: 'Sensor value: 169'
22+
# Set low light condition
23+
- set-control:
24+
part-id: photoresistor
25+
control: lux
26+
value: 100
27+
- delay: 500ms
28+
# Check low light values (analog value should be higher)
29+
- wait-serial: 'Sensor value: 511'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[env:uno]
2+
platform = atmelavr
3+
board = uno
4+
framework = arduino
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <Arduino.h>
2+
3+
const int PHOTORESISTOR_PIN = A0;
4+
5+
// These constants should match the photoresistor's "gamma" and "rl10" attributes
6+
const float GAMMA = 0.7;
7+
const float RL10 = 50;
8+
9+
void setup()
10+
{
11+
Serial.begin(115200);
12+
Serial.println("Photoresistor Sensor Test");
13+
}
14+
15+
void loop()
16+
{
17+
// Read the analog value from the photoresistor
18+
int lightValue = analogRead(PHOTORESISTOR_PIN);
19+
20+
// Convert to voltage (ESP32 ADC reference is 3.3V, 12-bit resolution)
21+
float voltage = (lightValue * 5) / 1024.0;
22+
23+
// Convert the analog value into lux value:
24+
float resistance = 2000 * voltage / (1 - voltage / 5);
25+
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
26+
27+
// Print the values
28+
Serial.print("Sensor value: ");
29+
Serial.print(lightValue);
30+
Serial.print(", Voltage: ");
31+
Serial.print(voltage);
32+
Serial.print(", Lux: ");
33+
Serial.println(lux);
34+
35+
// Wait before next reading
36+
delay(1000);
37+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[wokwi]
2+
version = 1
3+
firmware = '.pio/build/uno/firmware.elf'
4+
elf = '.pio/build/uno/firmware.elf'

0 commit comments

Comments
 (0)