parent
b672bd3c23
commit
71e08bf4e3
@ -0,0 +1,47 @@
|
||||
# Calibrate your sensor
|
||||
|
||||
## Instructions
|
||||
|
||||
In this lesson you gathered soil moisture sensor readings, measured as values from 0-1023. To convert these into actual soil moisture readings, you need to calibrate your sensor. You can do this by taking readings from soil samples, then calculating the gravimetric soil moisture content from these samples.
|
||||
|
||||
You will need to repeat these steps multiple times to get the readings needed, with different wetness of soil each time.
|
||||
|
||||
1. Take a soil moisture reading using the soil moisture sensor. Write down this reading.
|
||||
|
||||
1. Take a sample of the soil, and weigh it. Write down this weight.
|
||||
|
||||
1. Dry the soil - a warm oven at 110°C (230°F) for a few hours is the best way, you can do this in sunlight, or place it in a warm, dry place until the soil is completely dry. It should be powdery and loose.
|
||||
|
||||
> 💁 In a lab for the most accurate results you would dry in an oven for 48-72 hours. If you have drying ovens at your school, see if you can use these to dry for longer. The longer, the more dry the sample and the more accurate the results.
|
||||
|
||||
1. Weigh the soil again.
|
||||
|
||||
> 🔥 If you dried it in an oven make sure it has cooled down first!
|
||||
|
||||
The gravimetric soil moisture is calculated as:
|
||||
|
||||

|
||||
|
||||
* W<sub>wet</sub> - the weight of the wet soil
|
||||
* W<sub>dry</sub> - the weight of the dry soil
|
||||
|
||||
For example, say you have a soil sample that weights 212g wet, and 197g dry.
|
||||
|
||||

|
||||
|
||||
* W<sub>wet</sub> = 212g
|
||||
* W<sub>dry</sub> = 197g
|
||||
* 212 - 197 = 15
|
||||
* 15 / 197 = 0.076
|
||||
* 0.076 * 100 = 7.6%
|
||||
|
||||
In this example, the soil has a gravimetric soil moisture of 7.6%.
|
||||
|
||||
Once you have the readings for at least 3 samples, plot a graph of soil moisture % to soil moisture sensor reading and add line to best fit the points. You can then use this to calculate the gravimetric soil moisture content for a given sensor reading by reading the value from the line.
|
||||
|
||||
## Rubric
|
||||
|
||||
| Criteria | Exemplary | Adequate | Needs Improvement |
|
||||
| -------- | --------- | -------- | ----------------- |
|
||||
| Gather calibration data | Capture at least 3 calibration samples | Capture at least 2 calibration samples |Capture at least 1 calibration sample |
|
||||
| Make a calibrated reading | Successfully plot the calibration graph and make a reading from the sensor, and convert it to gravimetric soil moisture content | Successfully plot the calibration graph | Not able to plot the graph |
|
@ -0,0 +1,94 @@
|
||||
# Measure soil moisture - Raspberry Pi
|
||||
|
||||
In this part of the lesson, you will add a capacitive soil moisture sensor to your Raspberry Pi, and read values from it.
|
||||
|
||||
## Hardware
|
||||
|
||||
The Raspberry Pi needs a capacitive soil moisture sensor.
|
||||
|
||||
The sensor you'll use is a [Capacitive Soil Moisture Sensor](https://www.seeedstudio.com/Grove-Capacitive-Moisture-Sensor-Corrosion-Resistant.html), that measures soil moisture by detecting the capacitance of the soil, a property than changes as the soil moisture changes. As the soil moisture increases, the voltage decreases.
|
||||
|
||||
This is an analog sensor, so uses an analog pin, and the 10-bit ADC in the Grove Base Hat on the Pi to convert the voltage to a digital signal from 1-1,023. This is then sent over I<sup>2</sup>C via the GPIO pins on the Pi.
|
||||
|
||||
### Connect the soil moisture sensor
|
||||
|
||||
The Grove soil moisture sensor can be connected to the Raspberry Pi.
|
||||
|
||||
#### Task - connect the soil moisture sensor
|
||||
|
||||
Connect the soil moisture sensor.
|
||||
|
||||

|
||||
|
||||
1. Insert one end of a Grove cable into the socket on the soil moisture sensor. 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. Insert the soil moisture sensor into soil. It has a 'highest position line' - a white line across the sensor. Insert the sensor up to but not past this line.
|
||||
|
||||

|
||||
|
||||
## Program the soil moisture sensor
|
||||
|
||||
The Raspberry Pi can now be programmed to use the attached soil moisture sensor.
|
||||
|
||||
### Task - program the soil moisture sensor
|
||||
|
||||
Program the device.
|
||||
|
||||
1. Power up the Pi and wait for it to boot
|
||||
|
||||
1. Launch VS Code, either directly on the Pi, or connect via the Remote SSH extension.
|
||||
|
||||
> ⚠️ You can refer to [the instructions for setting up and launch VS Code in nightlight - lesson 1 if needed](../../../1-getting-started/lessons/1-introduction-to-iot/pi.md).
|
||||
|
||||
1. From the terminal, create a new folder in the `pi` users home directory called `soil-moisture-sensor`. Create a file in this folder called `app.py`.
|
||||
|
||||
1. Open this folder in VS Code
|
||||
|
||||
1. Add the following code to the `app.py` file to import some required libraries:
|
||||
|
||||
```python
|
||||
import time
|
||||
from grove.adc import ADC
|
||||
```
|
||||
|
||||
The `import time` statement imports the `time` module that will be used later in this assignment.
|
||||
|
||||
The `from grove.adc import ADC` statement imports the `ADC` from the Grove Python libraries. This library has code to interact with the analog to digital converter on the Pi base hat and read voltages from analog sensors.
|
||||
|
||||
1. Add the following code below this to create an instance of the `ADC` class:
|
||||
|
||||
```python
|
||||
adc = ADC()
|
||||
```
|
||||
|
||||
1. Add an infinite loop that reads from this ADC on the A0 pin, and write the result to the console. This loop can then sleep for 10 seconds between reads.
|
||||
|
||||
```python
|
||||
while True:
|
||||
soil_moisture = adc.read(0)
|
||||
print("Soil moisture:", soil_moisture)
|
||||
|
||||
time.sleep(10)
|
||||
```
|
||||
|
||||
1. Run the Python app. You will see the soil moisture measurements written to the console. Add some water to the soil, or remove the sensor from the soil, and see the value change.
|
||||
|
||||
```output
|
||||
pi@raspberrypi:~/soil-moisture-sensor $ python3 app.py
|
||||
Soil moisture: 615
|
||||
Soil moisture: 612
|
||||
Soil moisture: 498
|
||||
Soil moisture: 493
|
||||
Soil moisture: 490
|
||||
Soil Moisture: 388
|
||||
```
|
||||
|
||||
In the example output above, you can see the voltage drop as water is added.
|
||||
|
||||
> 💁 You can find this code in the [code/pi](code/pi) folder.
|
||||
|
||||
😀 Your soil moisture sensor program was a success!
|
@ -0,0 +1,109 @@
|
||||
# Measure soil moisture - Virtual IoT Hardware
|
||||
|
||||
In this part of the lesson, you will add a capacitive soil moisture sensor to your virtual IoT device, and read values from it.
|
||||
|
||||
## Virtual Hardware
|
||||
|
||||
The virtual IoT device will use a simulated Grove capacitive soil moisture sensor. This keeps this lab the same as using a Raspberry Pi with a physical Grove capacitive soil moisture sensor.
|
||||
|
||||
In a physical IoT device, the soil moisture sensor would be a capacitive sensor that measures soil moisture by detecting the capacitance of the soil, a property than changes as the soil moisture changes. As the soil moisture increases, the voltage decreases.
|
||||
|
||||
This is an analog sensor, so uses a simulated 10-bit ADC to report a value from 1-1,023.
|
||||
|
||||
### Add the soil moisture sensor to CounterFit
|
||||
|
||||
To use a virtual soil moisture sensor, you need to add it to the CounterFit app
|
||||
|
||||
#### Task - dd the soil moisture sensor to CounterFit
|
||||
|
||||
Add the soil moisture sensor to the CounterFit app.
|
||||
|
||||
1. Create a new Python app on your computer in a folder called `soil-moisture-sensor` with a single file called `app.py` and a Python virtual environment, and add the CounterFit pip packages.
|
||||
|
||||
> ⚠️ You can refer to [the instructions for creating and setting up a CounterFit Python project in lesson 1 if needed](../../../1-getting-started/lessons/1-introduction-to-iot/virtual-device.md).
|
||||
|
||||
1. Make sure the CounterFit web app is running
|
||||
|
||||
1. Create a soil moisture sensor:
|
||||
|
||||
1. In the *Create sensor* box in the *Sensors* pane, drop down the *Sensor type* box and select *Soil Moisture*.
|
||||
|
||||
1. Leave the *Units* set to *NoUnits*
|
||||
|
||||
1. Ensure the *Pin* is set to *0*
|
||||
|
||||
1. Select the **Add** button to create the humidity sensor on Pin 0
|
||||
|
||||

|
||||
|
||||
The soil moisture sensor will be created and appear in the sensors list.
|
||||
|
||||

|
||||
|
||||
## Program the soil moisture sensor app
|
||||
|
||||
The soil moisture sensor app can now be programmed using the CounterFit sensors.
|
||||
|
||||
### Task - program the soil moisture sensor app
|
||||
|
||||
Program the soil moisture sensor app.
|
||||
|
||||
1. Make sure the `soil-moisture-sensor` app is open in VS Code
|
||||
|
||||
1. Open the `app.py` file
|
||||
|
||||
1. Add the following code to the top of `app.py` to connect the app to CounterFit:
|
||||
|
||||
```python
|
||||
from counterfit_connection import CounterFitConnection
|
||||
CounterFitConnection.init('127.0.0.1', 5000)
|
||||
```
|
||||
|
||||
1. Add the following code to the `app.py` file to import some required libraries:
|
||||
|
||||
```python
|
||||
import time
|
||||
from counterfit_shims_grove.adc import ADC
|
||||
```
|
||||
|
||||
The `import time` statement imports the `time` module that will be used later in this assignment.
|
||||
|
||||
The `from counterfit_shims_grove.adc import ADC` statement imports the `ADC` class to interact with a virtual analog to digital converter that can connect to a CounterFit sensor.
|
||||
|
||||
1. Add the following code below this to create an instance of the `ADC` class:
|
||||
|
||||
```python
|
||||
adc = ADC()
|
||||
```
|
||||
|
||||
1. Add an infinite loop that reads from this ADC on pin 0 and write the result to the console. This loop can then sleep for 10 seconds between reads.
|
||||
|
||||
```python
|
||||
while True:
|
||||
soil_moisture = adc.read(0)
|
||||
print("Soil moisture:", soil_moisture)
|
||||
|
||||
time.sleep(10)
|
||||
```
|
||||
|
||||
1. From the CounterFit app, change the value of the soil moisture sensor that will be read by the app. You can do this in one of two ways:
|
||||
|
||||
* Enter a number in the *Value* box for the soil moisture sensor, then select the **Set** button. The number you enter will be the value returned by the sensor.
|
||||
|
||||
* Check the *Random* checkbox, and enter a *Min* and *Max* value, then select the **Set** button. Every time the sensor reads a value, it will read a random number between *Min* and *Max*.
|
||||
|
||||
1. Run the Python app. You will see the soil moisture measurements written to the console. Change the *Value* or the *Random* settings to see the value change.
|
||||
|
||||
```output
|
||||
(.venv) ➜ soil-moisture-sensor $ python app.py
|
||||
Soil moisture: 615
|
||||
Soil moisture: 612
|
||||
Soil moisture: 498
|
||||
Soil moisture: 493
|
||||
Soil moisture: 490
|
||||
Soil Moisture: 388
|
||||
```
|
||||
|
||||
> 💁 You can find this code in the [code/virtual-device](code/virtual-device) folder.
|
||||
|
||||
😀 Your soil moisture sensor program was a success!
|
@ -0,0 +1,103 @@
|
||||
# Measure soil moisture - Wio Terminal
|
||||
|
||||
In this part of the lesson, you will add a capacitive soil moisture sensor to your Wio Terminal, and read values from it.
|
||||
|
||||
## Hardware
|
||||
|
||||
The Wio Terminal needs a capacitive soil moisture sensor.
|
||||
|
||||
The sensor you'll use is a [Capacitive Soil Moisture Sensor](https://www.seeedstudio.com/Grove-Capacitive-Moisture-Sensor-Corrosion-Resistant.html), that measures soil moisture by detecting the capacitance of the soil, a property than changes as the soil moisture changes. As the soil moisture increases, the voltage decreases.
|
||||
|
||||
This is an analog sensor, so connects to analog pins on the Wio Terminal, using an onboard ADC to create a value from 0-1,023.
|
||||
|
||||
### Connect the soil moisture sensor
|
||||
|
||||
The Grove soil moisture sensor can be connected to the Wio Terminals configurable analog/digital port.
|
||||
|
||||
#### Task - connect the soil moisture sensor
|
||||
|
||||
Connect the soil moisture sensor.
|
||||
|
||||

|
||||
|
||||
1. Insert one end of a Grove cable into the socket on the soil moisture sensor. It will only go in one way round.
|
||||
|
||||
1. With the Wio Terminal disconnected from your computer or other power supply, connect the other end of the Grove cable to the right-hand side Grove socket on the Wio Terminal as you look at the screen. This is the socket farthest away from the power button.
|
||||
|
||||

|
||||
|
||||
1. Insert the soil moisture sensor into soil. It has a 'highest position line' - a white line across the sensor. Insert the sensor up to but not past this line.
|
||||
|
||||

|
||||
|
||||
1. You can now connect the Wio Terminal to your computer.
|
||||
|
||||
## Program the soil moisture sensor
|
||||
|
||||
The Wio Terminal can now be programmed to use the attached soil moisture sensor.
|
||||
|
||||
### Task - program the soil moisture sensor
|
||||
|
||||
Program the device.
|
||||
|
||||
1. Create a brand new Wio Terminal project using PlatformIO. Call this project `soil-moisture-sensor`. Add code in the `setup` function to configure the serial port.
|
||||
|
||||
> ⚠️ You can refer to [the instructions for creating a PlatformIO project in project 1, lesson 1 if needed](../../../1-getting-started/lessons/1-introduction-to-iot/wio-terminal.md#create-a-platformio-project).
|
||||
|
||||
1. There isn't a library for this sensor, instead you can read from the analog pin using the built in Arduino [`analogRead`](https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/) function. Start by configuring the analog pin for input so values can be read from it by adding the following to the `setup` function.
|
||||
|
||||
```cpp
|
||||
pinMode(A0, INPUT);
|
||||
```
|
||||
|
||||
This sets the `A0` pin, the combined analog/digital pin, as an input pin that voltage can be read from.
|
||||
|
||||
1. Add the following to the `loop` function to read the voltage from this pin:
|
||||
|
||||
```cpp
|
||||
int soil_moisture = analogRead(A0);
|
||||
```
|
||||
|
||||
1. Below this code, add the following code to print the value to the serial port:
|
||||
|
||||
```cpp
|
||||
Serial.print("Soil Moisture: ");
|
||||
Serial.println(soil_moisture);
|
||||
```
|
||||
|
||||
1. Finally add a delay at the end of 10 seconds:
|
||||
|
||||
```cpp
|
||||
delay(10000);
|
||||
```
|
||||
|
||||
1. Build and upload the code to the Wio Terminal.
|
||||
|
||||
> ⚠️ You can refer to [the instructions for creating a PlatformIO project in project 1, lesson 1 if needed](../../../1-getting-started/lessons/1-introduction-to-iot/wio-terminal.md#write-the-hello-world-app).
|
||||
|
||||
1. Once uploaded, you can monitor the soil moisture using the serial monitor. Add some water to the soil, or remove the sensor from the soil, and see the value change.
|
||||
|
||||
```output
|
||||
> Executing task: platformio device monitor <
|
||||
|
||||
--- Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time
|
||||
--- More details at http://bit.ly/pio-monitor-filters
|
||||
--- Miniterm on /dev/cu.usbmodem1201 9600,8,N,1 ---
|
||||
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
|
||||
Soil Moisture: 526
|
||||
Soil Moisture: 529
|
||||
Soil Moisture: 521
|
||||
Soil Moisture: 494
|
||||
Soil Moisture: 454
|
||||
Soil Moisture: 456
|
||||
Soil Moisture: 395
|
||||
Soil Moisture: 388
|
||||
Soil Moisture: 394
|
||||
Soil Moisture: 391
|
||||
```
|
||||
|
||||
In the example output above, you can see the voltage drop as water is added.
|
||||
|
||||
> 💁 You can find this code in the [code/wio-terminal](code/wio-terminal) folder.
|
||||
|
||||
😀 Your soil moisture sensor program was a success!
|
Loading…
Reference in new issue