Skip to content

Commit a689fd7

Browse files
Add beginner content (#401)
Co-authored-by: José Simões <[email protected]>
1 parent 843c3c5 commit a689fd7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+2725
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5+
</PropertyGroup>
6+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7+
<PropertyGroup>
8+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
9+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
10+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11+
<ProjectGuid>337ee449-ea4f-4ae6-8634-5faa582603d0</ProjectGuid>
12+
<OutputType>Exe</OutputType>
13+
<AppDesignerFolder>Properties</AppDesignerFolder>
14+
<FileAlignment>512</FileAlignment>
15+
<RootNamespace>AnalogRainSensor</RootNamespace>
16+
<AssemblyName>AnalogRainSensor</AssemblyName>
17+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
18+
</PropertyGroup>
19+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
20+
<ItemGroup>
21+
<Compile Include="Program.cs" />
22+
<Compile Include="Properties\AssemblyInfo.cs" />
23+
</ItemGroup>
24+
<ItemGroup>
25+
<Reference Include="mscorlib">
26+
<HintPath>..\packages\nanoFramework.CoreLibrary.1.15.5\lib\mscorlib.dll</HintPath>
27+
</Reference>
28+
<Reference Include="nanoFramework.Hardware.Esp32">
29+
<HintPath>..\packages\nanoFramework.Hardware.Esp32.1.6.23\lib\nanoFramework.Hardware.Esp32.dll</HintPath>
30+
</Reference>
31+
<Reference Include="nanoFramework.Runtime.Events">
32+
<HintPath>..\packages\nanoFramework.Runtime.Events.1.11.18\lib\nanoFramework.Runtime.Events.dll</HintPath>
33+
</Reference>
34+
<Reference Include="System.Device.Adc">
35+
<HintPath>..\packages\nanoFramework.System.Device.Adc.1.1.11\lib\System.Device.Adc.dll</HintPath>
36+
</Reference>
37+
</ItemGroup>
38+
<ItemGroup>
39+
<None Include="packages.config" />
40+
</ItemGroup>
41+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
42+
<ProjectExtensions>
43+
<ProjectCapabilities>
44+
<ProjectConfigurationsDeclaredAsItems />
45+
</ProjectCapabilities>
46+
</ProjectExtensions>
47+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Device.Adc;
5+
using System.Diagnostics;
6+
using System.Threading;
7+
using nanoFramework.Hardware.Esp32;
8+
9+
Debug.WriteLine("Hello from nanoFramework!");
10+
11+
AdcController adc1 = new AdcController();
12+
13+
// Get the reference minimum and maximum values
14+
int max1 = adc1.MaxValue;
15+
int min1 = adc1.MinValue;
16+
17+
Debug.WriteLine("min1=" + min1.ToString() + " max1=" + max1.ToString());
18+
19+
// We will use the pin 34 which is already setup as ADC1_CH6
20+
// If you want to change to use another pin, this function needs to be used to setup the pin
21+
// Note, that not all configurations are possible. You'll have to refer to the ESP32 Technical Reference Manual
22+
// or the board you're using to see which pins can be used as ADC.
23+
// Configuration.SetPinFunction(34, DeviceFunction.ADC1_CH6);
24+
25+
AdcChannel sensor = adc1.OpenChannel(6);
26+
27+
while (true)
28+
{
29+
// Read the raw value
30+
int rawValue = sensor.ReadValue();
31+
// Calculate the voltage
32+
// The ESP32 is using 3.3 V as the reference voltage
33+
// The ADC has a 12-bit resolution, so the maximum value is 4095
34+
// Still, using the AdcController.MaxValue and MinValue is a good practice
35+
double voltage = ((double)(rawValue - min1) / (max1 - min1)) * 3.3;
36+
Debug.WriteLine($"Raw Value: {rawValue}, Voltage: {voltage}");
37+
38+
// Here, it's more empirical, you can adjust to your own needs
39+
// You can also use directly the raw value to compare
40+
if (voltage < 1.5)
41+
{
42+
Debug.WriteLine("It's wet!");
43+
}
44+
else
45+
{
46+
Debug.WriteLine("It's dry!");
47+
}
48+
49+
Thread.Sleep(1000);
50+
}
51+
52+
Thread.Sleep(Timeout.Infinite);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("CSharp.BlankApplication")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("CSharp.BlankApplication")]
13+
[assembly: AssemblyCopyright("Copyright © 2024")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// Version information for an assembly consists of the following four values:
23+
//
24+
// Major Version
25+
// Minor Version
26+
// Build Number
27+
// Revision
28+
//
29+
// You can specify all the values or you can default the Build and Revision Numbers
30+
// by using the '*' as shown below:
31+
// [assembly: AssemblyVersion("1.0.*")]
32+
[assembly: AssemblyVersion("1.0.0.0")]
33+
[assembly: AssemblyFileVersion("1.0.0.0")]
+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
![moisture sensor](../Docs/moisture-sensor.png)
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).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
device
2+
beginner
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="nanoFramework.CoreLibrary" version="1.15.5" targetFramework="netnano1.0" />
4+
<package id="nanoFramework.Hardware.Esp32" version="1.6.23" targetFramework="netnano1.0" />
5+
<package id="nanoFramework.Runtime.Events" version="1.11.18" targetFramework="netnano1.0" />
6+
<package id="nanoFramework.System.Device.Adc" version="1.1.11" targetFramework="netnano1.0" />
7+
</packages>

samples/Beginner/Beginner.sln

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35506.116
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "1-BlinkLed", "BlinkLed\1-BlinkLed.nfproj", "{558B53B9-75E0-4C13-AFF1-8614B3E0BAA6}"
7+
EndProject
8+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "2-Button", "Button\2-Button.nfproj", "{3FA7A8F9-31FB-45B6-8727-D02C17D15BBA}"
9+
EndProject
10+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "3-ButtonNuget", "ButtonNuget\3-ButtonNuget.nfproj", "{5B17A480-3C0F-4D73-B941-E9F8C2A07925}"
11+
EndProject
12+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "5-Buzzer", "Buzzer\5-Buzzer.nfproj", "{9419FC61-403F-4E3D-BE93-B26D4A23B682}"
13+
EndProject
14+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "4-PwmLed", "PwmLed\4-PwmLed.nfproj", "{24D24536-E9B9-45B8-939A-0FB80EDBDD5D}"
15+
EndProject
16+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "6-ServoMotor", "ServoMotor\6-ServoMotor.nfproj", "{9D9F9AF9-8199-457B-89C9-4B4932EB2035}"
17+
EndProject
18+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "7-AnalogRainSensor", "AnalogRainSensor\7-AnalogRainSensor.nfproj", "{337EE449-EA4F-4AE6-8634-5FAA582603D0}"
19+
EndProject
20+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "8-TempHumI2c", "TempHumI2c\8-TempHumI2c.nfproj", "{755022BA-BA69-4B18-B9AD-5333010C9FC1}"
21+
EndProject
22+
Global
23+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
24+
Debug|Any CPU = Debug|Any CPU
25+
Release|Any CPU = Release|Any CPU
26+
EndGlobalSection
27+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
28+
{558B53B9-75E0-4C13-AFF1-8614B3E0BAA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{558B53B9-75E0-4C13-AFF1-8614B3E0BAA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{558B53B9-75E0-4C13-AFF1-8614B3E0BAA6}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
31+
{558B53B9-75E0-4C13-AFF1-8614B3E0BAA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
32+
{558B53B9-75E0-4C13-AFF1-8614B3E0BAA6}.Release|Any CPU.Build.0 = Release|Any CPU
33+
{558B53B9-75E0-4C13-AFF1-8614B3E0BAA6}.Release|Any CPU.Deploy.0 = Release|Any CPU
34+
{3FA7A8F9-31FB-45B6-8727-D02C17D15BBA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35+
{3FA7A8F9-31FB-45B6-8727-D02C17D15BBA}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{3FA7A8F9-31FB-45B6-8727-D02C17D15BBA}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
37+
{3FA7A8F9-31FB-45B6-8727-D02C17D15BBA}.Release|Any CPU.ActiveCfg = Release|Any CPU
38+
{3FA7A8F9-31FB-45B6-8727-D02C17D15BBA}.Release|Any CPU.Build.0 = Release|Any CPU
39+
{3FA7A8F9-31FB-45B6-8727-D02C17D15BBA}.Release|Any CPU.Deploy.0 = Release|Any CPU
40+
{5B17A480-3C0F-4D73-B941-E9F8C2A07925}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
41+
{5B17A480-3C0F-4D73-B941-E9F8C2A07925}.Debug|Any CPU.Build.0 = Debug|Any CPU
42+
{5B17A480-3C0F-4D73-B941-E9F8C2A07925}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
43+
{5B17A480-3C0F-4D73-B941-E9F8C2A07925}.Release|Any CPU.ActiveCfg = Release|Any CPU
44+
{5B17A480-3C0F-4D73-B941-E9F8C2A07925}.Release|Any CPU.Build.0 = Release|Any CPU
45+
{5B17A480-3C0F-4D73-B941-E9F8C2A07925}.Release|Any CPU.Deploy.0 = Release|Any CPU
46+
{9419FC61-403F-4E3D-BE93-B26D4A23B682}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
47+
{9419FC61-403F-4E3D-BE93-B26D4A23B682}.Debug|Any CPU.Build.0 = Debug|Any CPU
48+
{9419FC61-403F-4E3D-BE93-B26D4A23B682}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
49+
{9419FC61-403F-4E3D-BE93-B26D4A23B682}.Release|Any CPU.ActiveCfg = Release|Any CPU
50+
{9419FC61-403F-4E3D-BE93-B26D4A23B682}.Release|Any CPU.Build.0 = Release|Any CPU
51+
{9419FC61-403F-4E3D-BE93-B26D4A23B682}.Release|Any CPU.Deploy.0 = Release|Any CPU
52+
{24D24536-E9B9-45B8-939A-0FB80EDBDD5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
53+
{24D24536-E9B9-45B8-939A-0FB80EDBDD5D}.Debug|Any CPU.Build.0 = Debug|Any CPU
54+
{24D24536-E9B9-45B8-939A-0FB80EDBDD5D}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
55+
{24D24536-E9B9-45B8-939A-0FB80EDBDD5D}.Release|Any CPU.ActiveCfg = Release|Any CPU
56+
{24D24536-E9B9-45B8-939A-0FB80EDBDD5D}.Release|Any CPU.Build.0 = Release|Any CPU
57+
{24D24536-E9B9-45B8-939A-0FB80EDBDD5D}.Release|Any CPU.Deploy.0 = Release|Any CPU
58+
{9D9F9AF9-8199-457B-89C9-4B4932EB2035}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
59+
{9D9F9AF9-8199-457B-89C9-4B4932EB2035}.Debug|Any CPU.Build.0 = Debug|Any CPU
60+
{9D9F9AF9-8199-457B-89C9-4B4932EB2035}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
61+
{9D9F9AF9-8199-457B-89C9-4B4932EB2035}.Release|Any CPU.ActiveCfg = Release|Any CPU
62+
{9D9F9AF9-8199-457B-89C9-4B4932EB2035}.Release|Any CPU.Build.0 = Release|Any CPU
63+
{9D9F9AF9-8199-457B-89C9-4B4932EB2035}.Release|Any CPU.Deploy.0 = Release|Any CPU
64+
{337EE449-EA4F-4AE6-8634-5FAA582603D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
65+
{337EE449-EA4F-4AE6-8634-5FAA582603D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
66+
{337EE449-EA4F-4AE6-8634-5FAA582603D0}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
67+
{337EE449-EA4F-4AE6-8634-5FAA582603D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
68+
{337EE449-EA4F-4AE6-8634-5FAA582603D0}.Release|Any CPU.Build.0 = Release|Any CPU
69+
{337EE449-EA4F-4AE6-8634-5FAA582603D0}.Release|Any CPU.Deploy.0 = Release|Any CPU
70+
{755022BA-BA69-4B18-B9AD-5333010C9FC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
71+
{755022BA-BA69-4B18-B9AD-5333010C9FC1}.Debug|Any CPU.Build.0 = Debug|Any CPU
72+
{755022BA-BA69-4B18-B9AD-5333010C9FC1}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
73+
{755022BA-BA69-4B18-B9AD-5333010C9FC1}.Release|Any CPU.ActiveCfg = Release|Any CPU
74+
{755022BA-BA69-4B18-B9AD-5333010C9FC1}.Release|Any CPU.Build.0 = Release|Any CPU
75+
{755022BA-BA69-4B18-B9AD-5333010C9FC1}.Release|Any CPU.Deploy.0 = Release|Any CPU
76+
EndGlobalSection
77+
GlobalSection(SolutionProperties) = preSolution
78+
HideSolutionNode = FALSE
79+
EndGlobalSection
80+
EndGlobal

0 commit comments

Comments
 (0)