|
| 1 | +# 🌶️ - Read a soil moisture sensor with Analog to Digital Converter (ADC) |
| 2 | + |
| 3 | +Shows how to use the [System.Device.Adc](https://docs.nanoframework.net/api/System.Device.Adc.html) API to set GPIO pins. |
| 4 | + |
| 5 | +We are using a capacitive soil moisture sensor for this experiment. You can any kind of moisture sensor that are analogic. They will all work the same way. |
| 6 | + |
| 7 | +And if you want to know more about ADC, how this works, you can read the [All what you've always wanted to know about ADC](https://docs.nanoframework.net/content/getting-started-guides/adc-explained.html) content! |
| 8 | + |
| 9 | +It's important to note that in this example, we are using the pin 34 which is ADC1 and channel 6. For each board, when you want to use ADC, you have to check the notion of channel. They are usuaklly not the same as the GPIO numbers. Keep that in mind! |
| 10 | + |
| 11 | +So, just connect the measurement pin, here, the yellow one to pin 34, the red one to 3.3V and the black one to the ground. |
| 12 | + |
| 13 | +Also note that for a specific usage, you may have to calibrate your analogic sensor. You can adjust in the code as well to see when you reach a specific limit. Enjoy! |
| 14 | + |
| 15 | +## Running the sample |
| 16 | + |
| 17 | +Ensure you have all the [software requirements](../README.md#software-requirements). |
| 18 | + |
| 19 | +To build the sample, follow the section [here](../README.md#build-the-sample). And to run it, [here](../README.md#run-the-sample). |
| 20 | + |
| 21 | +The sample is [located here](./Program.cs). The code is very straightforward with the explanations: |
| 22 | + |
| 23 | +```csharp |
| 24 | +// Licensed to the .NET Foundation under one or more agreements. |
| 25 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 26 | +
|
| 27 | +using System.Device.Adc; |
| 28 | +using System.Diagnostics; |
| 29 | +using System.Threading; |
| 30 | +using nanoFramework.Hardware.Esp32; |
| 31 | + |
| 32 | +Debug.WriteLine("Hello from nanoFramework!"); |
| 33 | + |
| 34 | +AdcController adc1 = new AdcController(); |
| 35 | + |
| 36 | +// Get the reference minimul and maximum values |
| 37 | +int max1 = adc1.MaxValue; |
| 38 | +int min1 = adc1.MinValue; |
| 39 | + |
| 40 | +Debug.WriteLine("min1=" + min1.ToString() + " max1=" + max1.ToString()); |
| 41 | + |
| 42 | +// We will use the pin 34 which is already setup as ADC1_CH6 |
| 43 | +// If you want to change to use another pin, this function needs to be used to setup the pin |
| 44 | +// Note, that not all configurations are possible. You'll have to refer to the ESP32 Technical Reference Manual |
| 45 | +// or the board you're using to see which pins can be used as ADC. |
| 46 | +// Configuration.SetPinFunction(34, DeviceFunction.ADC1_CH6); |
| 47 | +
|
| 48 | +AdcChannel sensor = adc1.OpenChannel(6); |
| 49 | + |
| 50 | +while (true) |
| 51 | +{ |
| 52 | + // Read the raw value |
| 53 | + int rawValue = sensor.ReadValue(); |
| 54 | + // Calculate the voltage |
| 55 | + // The ESP32 is using 3.3 V as the reference voltage |
| 56 | + // The ADC has a 12-bit resolution, so the maximum value is 4095 |
| 57 | + // Still, using the AdcController.MaxValue and MinValue is a good practice |
| 58 | + double voltage = ((double)(rawValue - min1) / (max1 - min1)) * 3.3; |
| 59 | + Debug.WriteLine($"Raw Value: {rawValue}, Voltage: {voltage}"); |
| 60 | + |
| 61 | + // Here, it's more empirical, you can adjust to your own needs |
| 62 | + // You can also use directly the raw value to compare |
| 63 | + if (voltage < 1.5) |
| 64 | + { |
| 65 | + Debug.WriteLine("It's wet!"); |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + Debug.WriteLine("It's dry!"); |
| 70 | + } |
| 71 | + |
| 72 | + Thread.Sleep(1000); |
| 73 | +} |
| 74 | + |
| 75 | +Thread.Sleep(Timeout.Infinite); |
| 76 | +``` |
| 77 | + |
| 78 | +Place the sensor in water like this: |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +And as a result, you will see something like the following results. Note that it takes a bit of time to have the sensor adjusting to the change in humidity: |
| 83 | + |
| 84 | +```text |
| 85 | +min1=0 max1=4095 |
| 86 | +Raw Value: 3568, Voltage: 2.8753113553113554 |
| 87 | +It's dry! |
| 88 | +Raw Value: 3583, Voltage: 2.8873992673992674 |
| 89 | +It's dry! |
| 90 | +Raw Value: 3572, Voltage: 2.8785347985347984 |
| 91 | +It's dry! |
| 92 | +Raw Value: 3367, Voltage: 2.7133333333333329 |
| 93 | +It's dry! |
| 94 | +Raw Value: 3236, Voltage: 2.6077655677655676 |
| 95 | +It's dry! |
| 96 | +Raw Value: 3029, Voltage: 2.44095238095238 |
| 97 | +It's dry! |
| 98 | +Raw Value: 2859, Voltage: 2.3039560439560436 |
| 99 | +It's dry! |
| 100 | +Raw Value: 2768, Voltage: 2.23062271062271 |
| 101 | +It's dry! |
| 102 | +Raw Value: 2672, Voltage: 2.1532600732600731 |
| 103 | +It's dry! |
| 104 | +Raw Value: 2515, Voltage: 2.0267399267399266 |
| 105 | +It's dry! |
| 106 | +Raw Value: 2432, Voltage: 1.9598534798534796 |
| 107 | +It's dry! |
| 108 | +Raw Value: 2319, Voltage: 1.8687912087912086 |
| 109 | +It's dry! |
| 110 | +Raw Value: 2181, Voltage: 1.7575824175824176 |
| 111 | +It's dry! |
| 112 | +Raw Value: 2106, Voltage: 1.6971428571428568 |
| 113 | +It's dry! |
| 114 | +Raw Value: 2007, Voltage: 1.6173626373626373 |
| 115 | +It's dry! |
| 116 | +Raw Value: 1919, Voltage: 1.5464468864468863 |
| 117 | +It's dry! |
| 118 | +Raw Value: 1813, Voltage: 1.46102564102564 |
| 119 | +It's wet! |
| 120 | +Raw Value: 1715, Voltage: 1.382051282051282 |
| 121 | +It's wet! |
| 122 | +Raw Value: 1643, Voltage: 1.3240293040293041 |
| 123 | +It's wet! |
| 124 | +Raw Value: 1590, Voltage: 1.2813186813186812 |
| 125 | +It's wet! |
| 126 | +Raw Value: 1491, Voltage: 1.2015384615384614 |
| 127 | +It's wet! |
| 128 | +Raw Value: 1410, Voltage: 1.1362637362637362 |
| 129 | +It's wet! |
| 130 | +Raw Value: 1365, Voltage: 1.0999999999999999 |
| 131 | +It's wet! |
| 132 | +Raw Value: 1346, Voltage: 1.0846886446886446 |
| 133 | +It's wet! |
| 134 | +Raw Value: 1334, Voltage: 1.075018315018315 |
| 135 | +It's wet! |
| 136 | +Raw Value: 1329, Voltage: 1.070989010989011 |
| 137 | +It's wet! |
| 138 | +Raw Value: 1323, Voltage: 1.0661538461538462 |
| 139 | +It's wet! |
| 140 | +Raw Value: 1322, Voltage: 1.0653479853479853 |
| 141 | +It's wet! |
| 142 | +``` |
| 143 | + |
| 144 | +If you want to debug, follow the instructions [explained in the led sample](../BlinkLed//README.md#debugging). |
0 commit comments