Skip to content

Commit 64f1cfc

Browse files
author
Jim Bennett
authored
Sunlight sensor (#57)
* Adding content * Update en.json * Update README.md * Update TRANSLATIONS.md * Adding lesson tempolates * Fixing code files with each others code in * Update README.md * Migrating to sunlight sensor at the request of seeed * More on sunlight sensor
1 parent fa04074 commit 64f1cfc

File tree

22 files changed

+89
-103
lines changed

22 files changed

+89
-103
lines changed

1-getting-started/lessons/1-introduction-to-iot/code/wio-terminal/nightlight/app.py

-20
This file was deleted.

1-getting-started/lessons/1-introduction-to-iot/pi.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ If you are using a Raspberry Pi as your IoT hardware, you have two choices - you
1212

1313
Before you begin, you also need to connect the Grove Base Hat to your Pi.
1414

15-
### Task
15+
### Task - setup
1616

1717
Install the Grove base hat on your Pi and configure the Pi
1818

@@ -29,7 +29,7 @@ Install the Grove base hat on your Pi and configure the Pi
2929

3030
If you want to work directly on your Pi, you can use the desktop version of Raspberry Pi OS and install all the tools you need.
3131

32-
#### Task
32+
#### Task - work directly on your Pi
3333

3434
Set up your Pi for development.
3535

@@ -79,7 +79,7 @@ Rather than coding directly on the Pi, it can run 'headless', that is not connec
7979

8080
To code remotely, the Pi OS needs to be installed on an SD Card.
8181

82-
##### Task
82+
##### Task - set up the Pi OS
8383

8484
Set up the headless Pi OS.
8585

@@ -115,7 +115,7 @@ The OS will be written to the SD card, and once compete the card will be ejected
115115

116116
The next step is to remotely access the Pi. You can do this using `ssh`, which is available on macOS, Linux and recent versions of Windows.
117117

118-
##### Task
118+
##### Task - connect to the Pi
119119

120120
Remotely access the Pi.
121121

@@ -147,7 +147,7 @@ Remotely access the Pi.
147147
148148
Once you are connected to the Pi, you need to ensure the OS is up to date, and install various libraries and tools that interact with the Grove hardware.
149149
150-
##### Task
150+
##### Task - configure software on the Pi
151151
152152
Configure the installed Pi software and install the Grove libraries.
153153
@@ -179,7 +179,7 @@ Configure the installed Pi software and install the Grove libraries.
179179
180180
Once the Pi is configured, you can connect to it using Visual Studio Code (VS Code) from your computer - this is a free developer text editor you will be using to write your device code in Python.
181181
182-
##### Task
182+
##### Task - configure VS Code for remote access
183183
184184
Install the required software and connect remotely to your Pi.
185185
@@ -199,7 +199,7 @@ The Hello World app for the Pi will ensure that you have Python and Visual Studi
199199
200200
This app will be in a folder called `nightlight`, and it will be re-used with different code in later parts of this assignment to build the nightlight application.
201201
202-
### Task
202+
### Task - hello world
203203
204204
Create the Hello World app.
205205

1-getting-started/lessons/1-introduction-to-iot/virtual-device.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ Create a Python application to print `"Hello World"` to the console.
120120
code .
121121
```
122122
123-
> 💁 If your terminal returns `command not found` on macOS it means VS Code has not been added to PATH, you can add VS Code to PATH by following the instructions in the [Launching from the command line section of the VS Code documentation](https://code.visualstudio.com/docs/setup/mac?WT.mc_id=academic-17441-jabenn#_launching-from-the-command-line) and run the command afterwards. VS Code is installed to PATH by default on Windows and Linux.
123+
> 💁 If your terminal returns `command not found` on macOS it means VS Code has not been added to your PATH. You can add VS Code to yout PATH by following the instructions in the [Launching from the command line section of the VS Code documentation](https://code.visualstudio.com/docs/setup/mac?WT.mc_id=academic-17441-jabenn#_launching-from-the-command-line) and run the command afterwards. VS Code is installed to your PATH by default on Windows and Linux.
124124
125125
1. When VS Code launches, it will activate the Python virtual environment. You will see this in the bottom status bar:
126126

1-getting-started/lessons/1-introduction-to-iot/wio-terminal.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The [Wio Terminal from Seeed Studios](https://www.seeedstudio.com/Wio-Terminal-p
88

99
To use your Wio Terminal, you will need to install some free software on your computer. You will also need to update the Wio Terminal firmware before you can connect it to WiFi.
1010

11-
### Task
11+
### Task - setup
1212

1313
Install the required software and update the firmware.
1414

@@ -32,7 +32,7 @@ The Hello World app for the Wio Terminal will ensure that you have Visual Studio
3232

3333
The first step is to create a new project using PlatformIO configured for the Wio Terminal.
3434

35-
#### Task
35+
#### Task - create a PlatformIO project
3636

3737
Create the PlatformIO project.
3838

@@ -122,7 +122,7 @@ The VS Code explorer will show a number of files and folders created by the Plat
122122

123123
You're now ready to write the Hello World app.
124124
125-
#### Task
125+
#### Task - write the Hello World app
126126
127127
Write the Hello World app.
128128

1-getting-started/lessons/3-sensors-and-actuators/code-actuator/pi/nightlight/app.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import time
2-
from grove.grove_light_sensor_v1_2 import GroveLightSensor
2+
import seeed_si114x
33
from grove.grove_led import GroveLed
44

5-
light_sensor = GroveLightSensor(0)
5+
light_sensor = seeed_si114x.grove_si114x()
66
led = GroveLed(5)
77

88
while True:
9-
light = light_sensor.light
9+
light = light_sensor.ReadVisible
1010
print('Light level:', light)
1111

12-
if light < 200:
12+
if light < 300:
1313
led.on()
1414
else:
1515
led.off()

1-getting-started/lessons/3-sensors-and-actuators/code-actuator/virtual-device/nightlight/app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
light = light_sensor.light
1313
print('Light level:', light)
1414

15-
if light < 200:
15+
if light < 300:
1616
led.on()
1717
else:
1818
led.off()

1-getting-started/lessons/3-sensors-and-actuators/code-actuator/wio-terminal/nightlight/app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
light = light_sensor.light
1313
print('Light level:', light)
1414

15-
if light < 200:
15+
if light < 300:
1616
led.on()
1717
else:
1818
led.off()

1-getting-started/lessons/3-sensors-and-actuators/code-actuator/wio-terminal/nightlight/src/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void loop()
1919
Serial.print("Light value: ");
2020
Serial.println(light);
2121

22-
if (light < 200)
22+
if (light < 300)
2323
{
2424
digitalWrite(D0, HIGH);
2525
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import time
2-
from grove.grove_light_sensor_v1_2 import GroveLightSensor
2+
import seeed_si114x
33

4-
light_sensor = GroveLightSensor(0)
4+
light_sensor = seeed_si114x.grove_si114x()
55

66
while True:
7-
light = light_sensor.light
7+
light = light_sensor.ReadVisible
88
print('Light level:', light)
9-
109
time.sleep(1)

1-getting-started/lessons/3-sensors-and-actuators/code-sensor/wio-terminal/nightlight/app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
light = light_sensor.light
1313
print('Light level:', light)
1414

15-
if light < 200:
15+
if light < 300:
1616
led.on()
1717
else:
1818
led.off()

1-getting-started/lessons/3-sensors-and-actuators/pi-actuator.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The nightlight logic in pseudo-code is:
1212

1313
```output
1414
Check the light level.
15-
If the light is less than 200
15+
If the light is less than 300
1616
Turn the LED on
1717
Otherwise
1818
Turn the LED off
@@ -22,7 +22,7 @@ Otherwise
2222

2323
The Grove LED comes as a module with a selection of LEDs, allowing you to chose the color.
2424

25-
#### Task
25+
#### Task - connect the LED
2626

2727
Connect the LED.
2828

@@ -44,9 +44,9 @@ Connect the LED.
4444

4545
## Program the nightlight
4646

47-
The nightlight can now be programmed using the Grove light sensor and the Grove LED.
47+
The nightlight can now be programmed using the Grove sunlight sensor and the Grove LED.
4848

49-
### Task
49+
### Task - program the nightlight
5050

5151
Program the nightlight.
5252

@@ -70,16 +70,18 @@ Program the nightlight.
7070

7171
The line `led = GroveLed(5)` creates an instance of the `GroveLed` class connecting to pin **D5** - the digital Grove pin that the LED is connected to.
7272

73+
> 💁 All the sockets have unique pin numbers. Pins 0, 2, 4, and 6 are analog pins, pins 5, 16, 18, 22, 24, and 26 are digital pins.
74+
7375
1. Add a check inside the `while` loop, and before the `time.sleep` to check the light levels and turn the LED on or off:
7476

7577
```python
76-
if light < 200:
78+
if light < 300:
7779
led.on()
7880
else:
7981
led.off()
8082
```
8183

82-
This code checks the `light` value. If this is less than 200 it calls the `on` method of the `GroveLed` class which sends a digital value of 1 to the LED, turning it on. If the light value is greater than or equal to 200 it calls the `off` method, sending a digital value of 0 to the LED, turning it off.
84+
This code checks the `light` value. If this is less than 300 it calls the `on` method of the `GroveLed` class which sends a digital value of 1 to the LED, turning it on. If the light value is greater than or equal to 300 it calls the `off` method, sending a digital value of 0 to the LED, turning it off.
8385

8486
> 💁 This code should be indented to the same level as the `print('Light level:', light)` line to be inside the while loop!
8587

@@ -103,7 +105,7 @@ Program the nightlight.
103105
Light level: 290
104106
```
105107

106-
1. Cover and uncover the light sensor. Notice how the LED will light up if the light level is 200 or less, and turn off when the light level is greater than 200.
108+
1. Cover and uncover the sunlight sensor. Notice how the LED will light up if the light level is 300 or less, and turn off when the light level is greater than 300.
107109

108110
> 💁 If the LED doesn't turn on, make sure it is connected the right way round, and the spin button is set to full on.
109111

1-getting-started/lessons/3-sensors-and-actuators/pi-sensor.md

+36-28
Original file line numberDiff line numberDiff line change
@@ -4,70 +4,78 @@ In this part of the lesson, you will add a light sensor to your Raspberry Pi.
44

55
## Hardware
66

7-
The sensor for this lesson is a **light sensor** that uses a [photodiode](https://wikipedia.org/wiki/Photodiode) to convert light to an electrical signal. This is an analog sensor that sends an integer value from 0 to 1,023 indicating a relative amount of light that doesn't map to any standard unit of measurement such as [lux](https://wikipedia.org/wiki/Lux).
7+
The sensor for this lesson is a **sunlight sensor** that uses [photodiodes](https://wikipedia.org/wiki/Photodiode) to convert visible and infrared light to an electrical signal. This is an analog sensor that sends an integer value from 0 to 1,023 indicating a relative amount of light, but this can be used to calculate exact values in [lux](https://wikipedia.org/wiki/Lux) by taking data from the separate infrared and visible light sensors.
88

9-
The light sensor is an eternal Grove sensor and needs to be connected to the Grove Base hat on the Raspberry Pi.
9+
The sunlight sensor is an eternal Grove sensor and needs to be connected to the Grove Base hat on the Raspberry Pi.
1010

11-
### Connect the light sensor
11+
### Connect the sunlight sensor
1212

13-
The Grove light sensor that is used to detect the light levels needs to be connected to the Raspberry Pi.
13+
The Grove sunlight sensor that is used to detect the light levels needs to be connected to the Raspberry Pi.
1414

15-
#### Task
15+
#### Task - connect the sunlight sensor
1616

17-
Connect the light sensor
17+
Connect the sunlight sensor
1818

19-
![A grove light sensor](../../../images/grove-light-sensor.png)
19+
![A grove sunlight sensor](../../../images/grove-sunlight-sensor.png)
2020

21-
1. Insert one end of a Grove cable into the socket on the light sensor module. It will only go in one way round.
21+
1. Insert one end of a Grove cable into the socket on the sunlight sensor module. It will only go in one way round.
2222

23-
1. With the Raspberry Pi powered off, connect the other end of the Grove cable to the analog socket marked **A0** on the Grove Base hat attached to the Pi. This socket is the second from the right, on the row of sockets next to the GPIO pins.
23+
1. With the Raspberry Pi powered off, connect the other end of the Grove cable to one of the three the I<sup>2</sup>C sockets marked **I2C** on the Grove Base hat attached to the Pi. This socket is the second from the right, on the row of sockets next to the GPIO pins.
2424

25-
![The grove light sensor connected to socket A0](../../../images/pi-light-sensor.png)
25+
> 💁 I<sup>2</sup>C is a way sensors and actuators can communicate with an IoT device. It will be covered in more detail in a later lesson.
2626
27-
## Program the light sensor
27+
![The grove sunlight sensor connected to socket A0](../../../images/pi-sunlight-sensor.png)
2828

29-
The device can now be programmed using the Grove light sensor.
29+
## Program the sunlight sensor
3030

31-
### Task
31+
The device can now be programmed using the Grove sunlight sensor.
32+
33+
### Task - program the sunlight sensor
3234

3335
Program the device.
3436

3537
1. Power up the Pi and wait for it to boot
3638

3739
1. Open the nightlight project in VS Code that you created in the previous part of this assignment, either running directly on the Pi or connected using the Remote SSH extension.
3840

41+
1. Run the following command to install a pip package for working with the sunlight sensor:
42+
43+
```sh
44+
pip3 install seeed-python-si114x
45+
```
46+
47+
Not all the libraries for the Grove Sensors are installed with the Grove install script you used in an earlier lesson. Some need additional packages.
48+
3949
1. Open the `app.py` file and remove all code from it
4050

4151
1. Add the following code to the `app.py` file to import some required libraries:
4252

4353
```python
4454
import time
45-
from grove.grove_light_sensor_v1_2 import GroveLightSensor
55+
import seeed_si114x
4656
```
4757

4858
The `import time` statement imports the `time` module that will be used later in this assignment.
4959

50-
The `from grove.grove_light_sensor_v1_2 import GroveLightSensor` statement imports the `GroveLightSensor` from the Grove Python libraries. This library has code to interact with a Grove light sensor, and was installed globally during the Pi setup.
60+
The `import seeed_si114x` statement imports the `seeed_si114x` module that has code to interact with the Grove sunlight sensor.
5161

5262
1. Add the following code after the code above to create an instance of the class that manages the light sensor:
5363

5464
```python
55-
light_sensor = GroveLightSensor(0)
65+
light_sensor = seeed_si114x.grove_si114x()
5666
```
5767

58-
The line `light_sensor = GroveLightSensor(0)` creates an instance of the `GroveLightSensor` class connecting to pin **A0** - the analog Grove pin that the light sensor is connected to.
59-
60-
> 💁 All the sockets have unique pin numbers. Pins 0, 2, 4, and 6 are analog pins, pins 5, 16, 18, 22, 24, and 26 are digital pins.
68+
The line `light_sensor = seeed_si114x.grove_si114x()` creates an instance of the `grove_si114x` sunlight sensor class.
6169
6270
1. Add an infinite loop after the code above to poll the light sensor value and print it to the console:
6371
6472
```python
6573
while True:
66-
light = light_sensor.light
74+
light = light_sensor.ReadVisible
6775
print('Light level:', light)
6876
```
6977
70-
This will read the current light level on a scale of 0-1,023 using the `light` property of the `GroveLightSensor` class. This property reads the analog value from the pin. This value is then printed to the console.
78+
This will read the current sunlight level on a scale of 0-1,023 using the `ReadVisible` property of the `grove_si114x` class. This value is then printed to the console.
7179
7280
1. Add a small sleep of one second at the end of the `loop` as the light levels don't need to be checked continuously. A sleep reduces the power consumption of the device.
7381
@@ -81,16 +89,16 @@ Program the device.
8189
python3 app.py
8290
```
8391
84-
You should see light values being output to the console. Cover and uncover the light sensor to see the values change:
92+
You should see sunlight values being output to the console. Cover and uncover the sunlight sensor to see the values change:
8593
8694
```output
8795
pi@raspberrypi:~/nightlight $ python3 app.py
88-
Light level: 634
89-
Light level: 634
90-
Light level: 634
91-
Light level: 230
92-
Light level: 104
93-
Light level: 290
96+
Light level: 259
97+
Light level: 265
98+
Light level: 265
99+
Light level: 584
100+
Light level: 550
101+
Light level: 497
94102
```
95103
96104
> 💁 You can find this code in the [code-sensor/pi](code-sensor/pi) folder.

0 commit comments

Comments
 (0)