Arduino is at the same time a hardware and a software, and open hardware : you can copy it, change it for free, and an open source project software in a Integrated Development Environment (IDE). You have acces to everything all the data and hardware components and other software components and the source code, producing the software. Is the oposite to the propietary code and propitary hardware
Arduino software is considered a C++ derivative and at the same time a Processing related software
Processing is an open source software previous to Arduino software and compatible with Arduino containing many open source Processing libraries of computer vision or Arduino as examples :
This is the Arduino code corresponding to my humidty project in the general project of the agriculture robotis, in this case the objective is mesure the humidity of a plant and depending on the soil moisture or humidity, a red LED will be switch on in case of low humidity and a green LED will be on in case of high soil mosture.
Next step will be to change LEDS and to use water pump, relays to water the plant
My code for the YL-69 soil mosisture sensor and LEDS is the following :
int rainPin = A0;
/* In Arduino uno there are six analog inputs of ten bits (1024 levels) and in ESP-32 there are 20 analog inputs of 12 bits (4096 levels). We are going to calculate the resolution of the sensors and the analog inputs, taking into account that Arduino uno is at 5 V microcontroller and ESP-32 is a 3.3 V microcontroller. 3,3 V / 4096 = 0.00080566406 V = 0.806 mV , 5 V / 1024 = 0.0048828125 V = 4.88 mV.
Level 0 is 0 V in Arduino, level 1 is 4.88 mV, level 2 is 9.76 mV, level 500 is 2441 mV = 2,4 V and level 1023 is 5 V. Can i mesure 6 mV ? No, the solution is a better ADC (Analog digital converter of more bits) for example, ADS 1115 is a ADC of 16 bits (2^16 = 65536 levels) 5 V / 65536 * 1000 = 0.08 mV. The advantatge is that we are capable to detect more values (more accuracy). Raspberry PI v4 doesn't have any ADC that is any analog input, connecting ADS 1115 when is avaliable.
/*int means integer variable corresponding to a integer number A0, 6 and 7 are integer numbers, A0 is a special case. Double forware slash means comment in Arduino lenguage. rAINpIN, green LED are names of the variabes in order to identifay the PINS (conector of the Arduino). For
variables names we folow the camel case that is the ferst latter of the first name in lower cases and the second name in the ferst letter wil be upper case. (Other
types o other posiblities of conventions for writting variables are used in other lenguages at snake case "for-example-this" and in camel case is this: "for-ExempleThis" it is impaosible to tart a vairable name with a number woth a special characters corresponding to keywords used in intructions)*/
/*Other tipes of variables in Arduino are Boolean (true/false), byte (0-255), char (character -128 - +127), int (-32768 - +32767), long (cery long numbers) and float (floating pint numbers). Boolean, byte and char are 8 bits long, 2^8=256 different values. Integer is 16 bits (2^16=65536/) in length, and finally, long and float are 32 bits (2^32=4294967296) length. There are signed and unsigned variable, if the variable is signed the value is devided by 2 (exemple:2^16=65536/2= 32768 integuer variables will go from -32768 to 32767, one number is 0 this is why I am reducing the last number by 1 unit). I can declare a long variable for mesuring milliseconds or micrseconds, signde or unsigned?Wgat is correct? unsigned long time; means always posiive of time.*/
//When I declare a variable I create an space in the comuters memory with a name.
//It is interesting to declare varaibles of the right size.
//If I add before int the word "const" it makes impossible to change.
const int greenLED = 6;
contint redLED = 7;
// you can adjust the threshold value
int thresholdValue = 720;
/* A variable can be a global variable if it this defined at the beginig of the code or local if it is defined inside a fuction aor void block. If I do not assing a initial value to a global variable it will be 0 as a default value.*/
void setup(){
*/ Setup is a block of code or function including the general settings for example if the pin is an input or an output, the inicial values of my circuit the green and the red LED will be off or LOW. In the pinMode function we use the previous global variables with known names, example: pinMode(rainPin, INPUT); is the same as pinMode (A0, input); BECASUE IT IS EASIER TO UNDERSTAND, because we give this variable name to the A0 pin becasue is the ferst anlagoinput in the Arduino.*/
*/ Analloginput in Arduino uno is a 10-bit analago digital converter (ADC) that means 2^10=1024 values from 0 to 1023.In ESP-32 microcpntroller there are 12-bit ADC that means 2^12=4096 values from 0-4095, in ADS1115 is 16-bit ADC 2^16=65535
pinMode(rainPin, INPUT);
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
digitalWrite(greenLED, LOW);
digitalWrite(redLED, LOW);
Serial.begin(9600);
*/Serial begin, mean Serial Comunication in Between the Devices of microcontroler and personal computer (PC), and this is the sped in bits for second or bouds, other common speeds are: 57600, 115200 and 9600.
}
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(rainPin);
Serial.println(sensorValue);
if(sensorValue < thresholdValue){
Serial.println(" - Doesn't need watering");
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
*/Means to write a two state value (HIGH, LOW, and some time in 0 and 1) in digital inputs, we need to define first the pin mode as an input and outpud, deppending on the features fo the pin, for example; if the pin is A0, i can not put un digital, just analgoic, because from A0-A5 pin are pins that only are inputs values of 10 bits (1024 values).
*/Arduino: 5volts, ESP-32: 3.3volts.
*/ESP-32: Tiene 12 bits and 2^12=4096 values.
*/Resulotion capacity of distinguixin differents values are bigger ESP-32; 3,3/4096mV, Arduino;5/1024mV
}
else {
Serial.println(" - Time to water your plant");
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, LOW);
}
delay(500);
}
This is the image of the circuit :