Skip to content

Commit 554318d

Browse files
author
Vikhyat Goyal
committed
Add driver for grove soil moisture sensor
1 parent 6f982bf commit 554318d

File tree

1 file changed

+268
-0
lines changed

1 file changed

+268
-0
lines changed
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Grove Soil Moisture Sensor\n",
8+
"\n",
9+
"This example shows how to use the Grove Corrosion-resistant Capacitive Soil Moisture Sensor.\n",
10+
"The Grove Soil Moisture Sensor produces an analog signal, and requires an ADC. \n",
11+
"\n",
12+
"A Pynq Grove Adapter, or Pynq Shield and a Grove I2C ADC are required for this example. \n",
13+
"\n",
14+
"In the example, the ADC is initialized, a test read is done, and then the sensor is set to log a reading every 100 milliseconds. "
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"## Overview\n",
22+
"\n",
23+
"The Grove - Capacitive Moisture Sensor (Corrosion Resistant) is a soil moisture sensor based on capacitance changes. Compared with resistive sensors, capacitive sensors do not require direct exposure of the metal electrodes, which can significantly reduce the erosion of the electrodes. Hence, we call it Corrosion Resistant.\n",
24+
"\n",
25+
"It is important to note that this sensor can only qualitatively test the humidity of the soil and cannot measure quantitatively. Which means when the humidity of the soil rises, the value of the output decreases; conversely, when the humidity decreases, the output value becomes higher.\n",
26+
"\n"
27+
]
28+
},
29+
{
30+
"cell_type": "markdown",
31+
"metadata": {},
32+
"source": [
33+
"## Helpful Links\n",
34+
"\n",
35+
"[Sensor webpage](https://wiki.seeedstudio.com/Grove-Capacitive_Moisture_Sensor-Corrosion-Resistant/)\n",
36+
"\n",
37+
"[PMOD to Grove adapter webpage](https://store.digilentinc.com/pynq-grove-system-add-on-board/)"
38+
]
39+
},
40+
{
41+
"cell_type": "markdown",
42+
"metadata": {},
43+
"source": [
44+
"## Using the Sensor"
45+
]
46+
},
47+
{
48+
"cell_type": "markdown",
49+
"metadata": {},
50+
"source": [
51+
"## Attention\n",
52+
"\n",
53+
"The part of the sensor inserted into the soil cannot exceed the highest position line (the white line).\n",
54+
"\n"
55+
]
56+
},
57+
{
58+
"cell_type": "markdown",
59+
"metadata": {},
60+
"source": [
61+
"### For this example, the PYNQ PMOD to Grove adapter is connected to PMODA, and the grove ADC is connected to group 'G4' on the adapter.\n"
62+
]
63+
},
64+
{
65+
"cell_type": "markdown",
66+
"metadata": {},
67+
"source": [
68+
"### 1. Load the base Overlay"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 9,
74+
"metadata": {},
75+
"outputs": [],
76+
"source": [
77+
"from pynq.overlays.base import BaseOverlay\n",
78+
"base = BaseOverlay(\"base.bit\")"
79+
]
80+
},
81+
{
82+
"cell_type": "markdown",
83+
"metadata": {},
84+
"source": [
85+
"### 2. ADC read from sensor"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": 50,
91+
"metadata": {
92+
"scrolled": true
93+
},
94+
"outputs": [
95+
{
96+
"name": "stdout",
97+
"output_type": "stream",
98+
"text": [
99+
"2.029834 V\n"
100+
]
101+
}
102+
],
103+
"source": [
104+
"from pynq.lib.pmod import Grove_ADC\n",
105+
"from pynq.lib.pmod import PMOD_GROVE_G4 \n",
106+
"\n",
107+
"grove_adc = Grove_ADC(base.PMODA,PMOD_GROVE_G4)\n",
108+
"print(\"{} V\".format(round(grove_adc.read(),6)))"
109+
]
110+
},
111+
{
112+
"cell_type": "markdown",
113+
"metadata": {},
114+
"source": [
115+
"### 3. Scale Voltage value for ease of use"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": 51,
121+
"metadata": {
122+
"scrolled": true
123+
},
124+
"outputs": [
125+
{
126+
"name": "stdout",
127+
"output_type": "stream",
128+
"text": [
129+
"686\n"
130+
]
131+
}
132+
],
133+
"source": [
134+
"# We assume output is 1-2.5V\n",
135+
"# We scale it to 0-1000 for \"ease\" of use\n",
136+
"\n",
137+
"v = round(grove_adc.read(),6) - 1\n",
138+
"if v<0:\n",
139+
" print(\"Sensor not connected\")\n",
140+
"else:\n",
141+
" print(int(v* 666.666))"
142+
]
143+
},
144+
{
145+
"cell_type": "markdown",
146+
"metadata": {},
147+
"source": [
148+
"### 4. Starting logging once every 100 milliseconds and print recorded values"
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": 75,
154+
"metadata": {},
155+
"outputs": [],
156+
"source": [
157+
"grove_adc.set_log_interval_ms(100)\n",
158+
"grove_adc.start_log()"
159+
]
160+
},
161+
{
162+
"cell_type": "code",
163+
"execution_count": 76,
164+
"metadata": {},
165+
"outputs": [
166+
{
167+
"name": "stdout",
168+
"output_type": "stream",
169+
"text": [
170+
"[0.0938, 0.0938, 0.0938, 0.0938, 0.0938, 0.0938, 0.0938, 0.0938, 0.0938, 0.0938, 0.0938, 0.0938, 0.0938, 0.0938, 0.0938, 0.0938, 0.0938, 0.0938, 0.0938, 0.0938, 0.0938, 0.0938, 0.0938, 0.0938]\n"
171+
]
172+
}
173+
],
174+
"source": [
175+
"log = grove_adc.get_log()\n",
176+
"print(log)\n",
177+
"\n"
178+
]
179+
},
180+
{
181+
"cell_type": "markdown",
182+
"metadata": {},
183+
"source": [
184+
"### 5. Get the average voltage value and scale it\n"
185+
]
186+
},
187+
{
188+
"cell_type": "code",
189+
"execution_count": 77,
190+
"metadata": {},
191+
"outputs": [
192+
{
193+
"name": "stdout",
194+
"output_type": "stream",
195+
"text": [
196+
"0.09379999999999994\n",
197+
"-604\n"
198+
]
199+
}
200+
],
201+
"source": [
202+
"av = sum(log)/len(log)\n",
203+
"print(av)\n",
204+
"final = int((round(av,6) - 1)*666.666)\n",
205+
"print(final)"
206+
]
207+
},
208+
{
209+
"cell_type": "markdown",
210+
"metadata": {},
211+
"source": [
212+
"### 6. Final humidity output"
213+
]
214+
},
215+
{
216+
"cell_type": "code",
217+
"execution_count": 78,
218+
"metadata": {},
219+
"outputs": [
220+
{
221+
"name": "stdout",
222+
"output_type": "stream",
223+
"text": [
224+
"Sensor is not connected.\n"
225+
]
226+
}
227+
],
228+
"source": [
229+
"if (final >0 and final <300):\n",
230+
" print (\"Soil Humidity is high.\")\n",
231+
"elif (final >300 and final <600):\n",
232+
" print (\"Soil Humidity is normal.\")\n",
233+
"elif final >600:\n",
234+
" print (\"Soil Humidity is low. Watering is required.\")\n",
235+
"else:\n",
236+
" print (\"Sensor is not connected.\")"
237+
]
238+
},
239+
{
240+
"cell_type": "code",
241+
"execution_count": null,
242+
"metadata": {},
243+
"outputs": [],
244+
"source": []
245+
}
246+
],
247+
"metadata": {
248+
"kernelspec": {
249+
"display_name": "Python 3",
250+
"language": "python",
251+
"name": "python3"
252+
},
253+
"language_info": {
254+
"codemirror_mode": {
255+
"name": "ipython",
256+
"version": 3
257+
},
258+
"file_extension": ".py",
259+
"mimetype": "text/x-python",
260+
"name": "python",
261+
"nbconvert_exporter": "python",
262+
"pygments_lexer": "ipython3",
263+
"version": "3.6.5"
264+
}
265+
},
266+
"nbformat": 4,
267+
"nbformat_minor": 1
268+
}

0 commit comments

Comments
 (0)