Arduino is at the same time a hardware and a software, and open hardware: you can copy it, change it, for free and open source software in an integrated develpoment enviornment (IDE). You have access to everything all the data and hardware components and all the software components and the soure code (codi font)producing the software. It's the oppostie to the propietary code and propietary hardware
Arduino software is condidered a c++ derivative and at the same time Processing related software
Processign is an open source software previous to Arduino software and compatible with Arduino, containing many open source Processing libaries of compter vision or Arduino as examples:
This is the Arduio code corresponding to my humidity project in the general project of agriculture robotics, the objective in this case is to mesure the humidty of a plant and depending on the soil moisture or humidity. A red LED will be switched on in case of low humidity and a green LED will be on in case of high soil moisture
Next step will be to change LEDS and to use water pump, relays to water the plant
My code for the YL-69 soil moisture sensor and LEDS is the following:
/* int means integer variable corresponding to an integer number A0, 6 and 7 are integer numbers, A0 is an especial case.
Double forward slash means comment in Arduino lenguaje.
RainPin, greenLED and redLED are names of the variable invented in order to identify the PINS( connectors of the Arduino).
For variables names we follow the camel case that is the first letter of the first name in lower cases and the second name in the first letter will be upper cas (other types posibilities of conventions for writing variable are used in other languajes are snake case "for-example-this" and in camel case will be "forExampleThis".
It is imposible to start a variable name with a number or with special character corresponding to key words used in instructions. */
/* Other types of variables in Arduino boolean(true/false), byte (0-255), char (character, -128 - +127), int (-32768 - +32767), long (very long numbers) and float (floating point numbers). Boolean, byte and char are 8 bits long, 2^8=256 different values. Integer is 16 bits (2^16=65535)in length, and finally long and float are 32(2^32=4294967296) bits long. There are signed an unsigned variables, if the variable is signed tha value is devided by 2 (example: 2^16=65535/2=32768) integer variables will go from -32768 to +32767, one number is 0, this is why I'm reducing the last number. I can declare a long variable for mesuring miliseconds o microseconds, signed or unsigned? Unsigned long time; means always positive for time.*/
// When I declare a variable I create a space in the computer memory with a name.
// It's interesting to decalre variables of the right size.
// If I add before int the word const it makes impossible to change
const int rainPin = A0;
/*In arduino 1 there are six analog inputs of ten bits(1024 levels) and in ESP-32 there are twenty 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 5v microcontroller and ESP-32 is 3.3V microcontroller.
3,3V / 4096 = 0,00080566406V = 0,806 mV; 5V / 1024 = 0.0048828125 V = 4,88 mV.
Level 0 is 0V in Arduino, level 1 is 4.88mV, level 2 is 9.76, level 500 is 2440 mV = 2,4 V and level 1023 is 5V. Can I mesure 6 mV? No, the solution is a better ADC (analog digitl converter of more bits) for example, ADS 1115 is a ADC of 16 bits (2^16 = 65536 levels) 5V / 65536 = 0.000076, 0.000076*1000= 0.08 mV. The advantage is that we are capable to detect more values (more accuracy) Raspberry PI version 4 doesn't have any ADC that is any analog input, connecting ADS 1115 when is available.
const int greenLED = 6;
const int redLED = 7;
// you can adjust the threshold value
int thresholdValue = 300;
/* A variable can be a global variable if it is defined at the begining of the code or local if it is defined inside a function or void block. Do not assign an initial value to a 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 initial 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, for exmple pinMode (rainPin, INPUT); is the same as pinMode(A0, INPUT); because it is easier to understand, because we give this variable name to tha A0 pin because is the first analog input in the Arduino.*/
/* Analog input in Arduino 1 is a 10-bit analog to digital converter (ADC) that mens 2^10=1024 values from 0 to 1023. In ESP-32 microcontroller there are 12-bit ADC that means 2^12=4096 values form 0 to 4095, in ADS1115 is 16-bit ADC 2^16=65536 values from 0 to 65535.*/
pinMode(rainPin, INPUT);
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
digitalWrite(greenLED, LOW);
digitalWrite(redLED, LOW);
Serial.begin(9600);
// Serial begin it means a serial communication beetwen devices (micro controlers and personal computer) and the number 9600 is the speed it bits per second or bouds other common speeds are 57600, and 115200
// Digital write it means to write a two state value HIGH and LOW sometimes 0,1 we need to define first the pin mode as input or output depending on the features of the pin for example if the pin is an A0, and A means analog input, it doesn`t accept output ( A0 - A5 pins of 10 bits, 1024 values ) ESP32 has 12 bits so it has 4096 values ( it's more powerfull ) maximum value 5V = 1024 values, and 3,3V = 4095 values. Resolution: capacity of distinguishing diferents values 3,3/4096 MV
}
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);
}
else {
Serial.println(" - Time to water your plant");
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, LOW);
}
delay(500);
}
This is the image of the circuit: