@ -12,7 +12,7 @@ If you are using a Raspberry Pi as your IoT hardware, you have two choices - you
Before you begin, you also need to connect the Grove Base Hat to your Pi.
Before you begin, you also need to connect the Grove Base Hat to your Pi.
### Task
### Task - setup
Install the Grove base hat on your Pi and configure the Pi
Install the Grove base hat on your Pi and configure the Pi
@ -29,7 +29,7 @@ Install the Grove base hat on your Pi and configure the Pi
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.
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.
#### Task
#### Task - work directly on your Pi
Set up your Pi for development.
Set up your Pi for development.
@ -79,7 +79,7 @@ Rather than coding directly on the Pi, it can run 'headless', that is not connec
To code remotely, the Pi OS needs to be installed on an SD Card.
To code remotely, the Pi OS needs to be installed on an SD Card.
##### Task
##### Task - set up the Pi OS
Set up the headless Pi OS.
Set up the headless Pi OS.
@ -115,7 +115,7 @@ The OS will be written to the SD card, and once compete the card will be ejected
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.
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.
##### Task
##### Task - connect to the Pi
Remotely access the Pi.
Remotely access the Pi.
@ -147,7 +147,7 @@ Remotely access the Pi.
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.
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.
##### Task
##### Task - configure software on the Pi
Configure the installed Pi software and install the Grove libraries.
Configure the installed Pi software and install the Grove libraries.
@ -179,7 +179,7 @@ Configure the installed Pi software and install the Grove libraries.
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.
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.
##### Task
##### Task - configure VS Code for remote access
Install the required software and connect remotely to your Pi.
Install the required software and connect remotely to your Pi.
@ -199,7 +199,7 @@ The Hello World app for the Pi will ensure that you have Python and Visual Studi
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.
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.
@ -120,7 +120,7 @@ Create a Python application to print `"Hello World"` to the console.
code .
code .
```
```
> 💁 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.
> 💁 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.
1. When VS Code launches, it will activate the Python virtual environment. You will see this in the bottom status bar:
1. When VS Code launches, it will activate the Python virtual environment. You will see this in the bottom status bar:
The Grove LED comes as a module with a selection of LEDs, allowing you to chose the color.
The Grove LED comes as a module with a selection of LEDs, allowing you to chose the color.
#### Task
#### Task - connect the LED
Connect the LED.
Connect the LED.
@ -44,9 +44,9 @@ Connect the LED.
## Program the nightlight
## Program the nightlight
The nightlight can now be programmed using the Grove light sensor and the Grove LED.
The nightlight can now be programmed using the Grove sunlight sensor and the Grove LED.
### Task
### Task - program the nightlight
Program the nightlight.
Program the nightlight.
@ -70,6 +70,8 @@ Program the nightlight.
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.
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.
> 💁 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.
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:
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:
```python
```python
@ -103,7 +105,7 @@ Program the nightlight.
Light level: 290
Light level: 290
```
```
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.
1. Cover and uncover the sunlight 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.
> 💁 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.
> 💁 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.
@ -4,31 +4,33 @@ In this part of the lesson, you will add a light sensor to your Raspberry Pi.
## Hardware
## Hardware
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).
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.
The light sensor is an eternal Grove sensor and needs to be connected to the Grove Base hat on the Raspberry Pi.
The sunlight sensor is an eternal Grove sensor and needs to be connected to the Grove Base hat on the Raspberry Pi.
### Connect the light sensor
### Connect the sunlight sensor
The Grove light sensor that is used to detect the light levels needs to be connected to the Raspberry Pi.
The Grove sunlight sensor that is used to detect the light levels needs to be connected to the Raspberry Pi.
#### Task
#### Task - connect the sunlight sensor
Connect the light sensor
Connect the sunlight sensor


1. Insert one end of a Grove cable into the socket on the light sensor module. It will only go in one way round.
1. Insert one end of a Grove cable into the socket on the sunlight sensor module. It will only go in one way round.
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.
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.

> 💁 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.
## Program the light sensor

The device can now be programmed using the Grove light sensor.
## Program the sunlight sensor
### Task
The device can now be programmed using the Grove sunlight sensor.
### Task - program the sunlight sensor
Program the device.
Program the device.
@ -36,38 +38,44 @@ Program the device.
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.
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.
1. Run the following command to install a pip package for working with the sunlight sensor:
```sh
pip3 install seeed-python-si114x
```
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.
1. Open the `app.py` file and remove all code from it
1. Open the `app.py` file and remove all code from it
1. Add the following code to the `app.py` file to import some required libraries:
1. Add the following code to the `app.py` file to import some required libraries:
```python
```python
import time
import time
from grove.grove_light_sensor_v1_2 import GroveLightSensor
import seeed_si114x
```
```
The `import time` statement imports the `time` module that will be used later in this assignment.
The `import time` statement imports the `time` module that will be used later in this assignment.
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.
The `import seeed_si114x` statement imports the `seeed_si114x` module that has code to interact with the Grove sunlight sensor.
1. Add the following code after the code above to create an instance of the class that manages the light sensor:
1. Add the following code after the code above to create an instance of the class that manages the light sensor:
```python
```python
light_sensor = GroveLightSensor(0)
light_sensor = seeed_si114x.grove_si114x()
```
```
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.
The line `light_sensor = seeed_si114x.grove_si114x()` creates an instance of the `grove_si114x` sunlight sensor class.
> 💁 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.
1. Add an infinite loop after the code above to poll the light sensor value and print it to the console:
1. Add an infinite loop after the code above to poll the light sensor value and print it to the console:
```python
```python
while True:
while True:
light = light_sensor.light
light = light_sensor.ReadVisible
print('Light level:', light)
print('Light level:', light)
```
```
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.
This will read the current light 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.
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.
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.
@ -81,16 +89,16 @@ Program the device.
python3 app.py
python3 app.py
```
```
You should see light values being output to the console. Cover and uncover the light sensor to see the values change:
You should see sunlight values being output to the console. Cover and uncover the sunlight sensor to see the values change:
```output
```output
pi@raspberrypi:~/nightlight $ python3 app.py
pi@raspberrypi:~/nightlight $ python3 app.py
Light level: 634
Light level: 259
Light level: 634
Light level: 265
Light level: 634
Light level: 265
Light level: 230
Light level: 584
Light level: 104
Light level: 550
Light level: 290
Light level: 497
```
```
> 💁 You can find this code in the [code-sensor/pi](code-sensor/pi) folder.
> 💁 You can find this code in the [code-sensor/pi](code-sensor/pi) folder.