This is designed to let you learn a little or a lot depending on your goals, it is designed to follow each step to build knowledge on the previous steps, however feel free to use it however it works best for you!
The information is presented in levels that allow you to build a contraption that measures moisture in soil.
However the same principles apply so if you learn how to get readings from an analog moisture sensor you'll also know it's the same process to use an analog distance sensor.
Completing level 1 will provide you the knowledge to use any analog sensor (light, sound, distance, smoke, alchohol, etc.).
Level 2 you also have human visible output (LEDs), again the use case here is LED lights, but once you acquire the knowledge to use a DIO pin it is the same to power an led as it is to turn on relay to control a lamp, lock, piston, pneumatics etc.
level 3 your contraption can use a servo motor to perform precisly controlled actions based on the external stimulus ie - if the soil is too dry, it can add just the right amount of water.
void setup() { Serial.begin(9600); } void loop() { Serial.println(analogRead(A0)); delay(1000); }
void setup() and void loop() are built into every Arduino program. void setup() runs whatever code is found within its brackets {} ONE TIME and is used to set any conditions that only need to be set once at the beginning of the code execution, and void loop() runs whatever code is found within its brackets {} repeatedly forever - as long as it is not interupted or disconnected from its power source (depending on the time it takes to run the code (an empty loop can run up to 4,000,000 times per second!))
Inside the void setup() brackets, you'll see only one line: Serial.begin(9600);. This line is a built in feature of Arduino that sets up Serial communication between the Arduino and Your computer over the USB cable (USB stands for Universal Serial Bus). You don't need to know the inner workings of it to be able to use it!
Inside the void loop() brackets, you'll see two lines: Serial.println(analogRead(A0)); and delay(1000);
The first line performs multiple actions Serial.println() will print to your "Serial Monitor" (your computer screen) whatever is inside the parenthesis, inside the parenthesis we see analogRead(A0), analogRead() is a built in arduino function that gets the reading from the pin specified inside its paranthises. In this case the A0 pin is the pin we connected between the Arduino and the Moisture sensor in our very first step!
The second line delay() simply pauses or "delays" for the number of milliseconds inside the parenthesis. 1000 milliseconds is equal to 1 second.
The Analog reading we see printing out on the Serial Monitor should be showing us readings similar in results to ~580 when dry to ~290 when wet (each individual sensor may have a slightly different values and calibration)
It's measuring the resistance to electrons flowing across the sensor (water provides less resistance) so more moisture means less resistance
Let's convert this to a percentage - a value that makes more sense to people:
float percentage = map(analogRead(A0), 290, 580, 100, 0);
Serial.println(percentage);
The left side of the equal sign float percentage creates a variable named "percentage" that is of type float
In C++ or Arduino, there are various data types like string, int, char, bool, float, and double. Each of these data types have specific values that can be stored in them.
char A char variable stores single characters, such as 'a' or 'B'.
string A string variable contains a collection of characters
int An integer variable - holds whole numbers (no decimals)
float or a double When we want to store numbers with decimals, we can either use the float or double
The byte size for float is 4 while the byte size for double is 8
a float can hold up to 7 decimal digits accurately while a double can hold up to 15
bool A boolean variable holds either a True or False and must be one of those two states.
The right side of the equal sign map(analogRead(A0), 290, 580, 100, 0); uses map() to associate our highest moisture reading (290) and our lowest moisture reading (580) to their respective readings as a percentage 100 (as our highest reading) - 0 (as our lowest reading).
The result of the map() is set as the value of the variable named percentage.
The Serial.println(percentage); Will print out our human readable percentage to the serial monitor
Your updated code should now looks like this:
void setup() { Serial.begin(9600); } void loop() { float percentage = map(analogRead(A0), 290, 580, 100, 0); Serial.println(percentage); delay(1000); }
Upload and view your updated code which should now be showing percentages: 0 for dry, and 100 for wet (Some adjustments may be necessary to your high and low value readings in map() due to calibration differences of the sensors and external factors like the current humidity)
Now that we are getting readings from our moisture sensor to our computer, lets replace the output going to the computer with led lights that will allow us to know the status of our moisture sensor.
The can bus allows us to drastically reduce the wiring by daisy chaining the components together in series instead of running wires directly from the roborio to each device.
The pins on an arduino are similar to the pins on a Roborio and come in three different flavors: