Molemi IoT
  • Space farming using food computers for sustainable farming
  • Introduction to Arduino
    • Introduction to Electronics
    • The Breadboard
  • Controlling LEDs with an Arduino
    • The Code
    • Adding a button to control the LEDs
      • The Code
  • Introducing Sensors
    • The CO2 and air quality sensor
    • The Code
      • Explaining the Code
    • Light Intensity Sensor
  • Introducing the NodeMCU
    • Getting Started
      • A simple test for ESP8266
        • The Code
    • A light bulb switch using NodeMCU and the Blynk app
      • Setup Blynk on your Smartphone
      • The Code
    • Controlling a Centurion gate Motor with NodeMCU a 2-channel relay
      • Installing Blynk in Arduino IDE
    • Read and display the water flow sensor on Blynk
      • Setting up Blynk
      • The Code
        • Explaining the code
    • Read and Display temperature sensor readings with NodeMCU.
      • Installing libraries for DS18B20 temperature sensor.
      • Displaying the sensor readings on the serial port
        • Getting temperature readings from different DS18B20 sensors.
          • The Code
            • Display sensor readings on a web server
              • Build the web server
                • Designing and building the web page
                • The Code
    • Display the DHT11 sensor reading on a web server using NodeMCU.
      • Monitoring Room Temp & Humidity using Blynk
      • Installing DHT library on the ESP8266
        • Installing the Asynchronous Web Server library
          • The Code
          • Designing and building the web page
  • Data Science for Farming
    • Getting Started with Colaboratory
    • Introduction to Python for DS using Colab
  • Machine Learning for Farming
  • Molemi Personal Food Computer
    • Bill of Materials
  • Setting up WaziGate on a Raspberry Pi
  • Setting up DHT11 on Raspberry Pi
  • Using Telegram To Control Outputs
Powered by GitBook
On this page

Was this helpful?

  1. Introducing Sensors

The Code

Constants are similar to variables in that they allow you to uniquely name things in the program, but unlike variables they cannot change. Name the analog input for easy reference, and create another named constant to hold the baseline temperature.

const int sensorPin = A0; 
const float baselineTemp = 20.0;

void setup() {
  // put your setup code here, to run once:
  
}

void loop() {
  // put your main code here, to run repeatedly:

}

For every 2 degrees above this baseline, the LED will turn on. The temperature is being stored as a float, or floating-point number. This type of number has a decimal point, and is used for numbers that can be expressed as fractions.

In the setup you’re going to use a new command, Serial. begin(). This opens up a connection between the Arduino and the computer, so you can see the values from the analog input on your computer screen.

The argument 9600 is the speed at which the Arduino will communicate, 9600 bits per second. You will use the Arduino IDE’s serial monitor to view the information you choose to send from your microcontroller. When you open the IDE’s serial monitor verify that the baud rate is 9600.

const int sensorPin = A0; 
const float baselineTemp = 20.0;

void setup() {
  // put your setup code here, to run once:
  
  Serial.begin(9600);
  
}

void loop() {
  // put your main code here, to run repeatedly:

}

Next up is a for() loop to set some pins as outputs. These are the pins that you attached LEDs to earlier. Instead of giving them unique names and typing out the pinMode() function for each one, you can use a for() loop to go through them all quickly. This is a handy trick if you have a large number of similar things you wish to iterate through in a program. Tell the for() loop to run through pins 2 to 4 sequentially.

The function Serial.print() sends information from the Arduino to a connected computer. You can see this information in your serial monitor. If you give Serial.print() an argument in quotation marks, it will print out the text you typed. If you give it a variable as an argument, it will print out the value of that variable.

const int sensorPin = A0; 
const float baselineTemp = 20.0;

void setup() {
  // put your setup code here, to run once:
  
  Serial.begin(9600); // open a serial port 
  for(int pinNumber = 2; pinNumber<5; pinNumber++){ 
  pinMode(pinNumber,OUTPUT);
  digitalWrite(pinNumber, LOW); 

}

void loop() {
  // put your main code here, to run repeatedly:
  
  int sensorVal = analogRead(sensorPin);
  Serial.print(“Sensor Value: “); 
  Serial.print(sensorVal);
   // convert the ADC reading to voltage
  float voltage = (sensorVal/1024.0) * 5.0;
  Serial.print(“, Volts: “); 
  Serial.print(voltage);
  Serial.print(“, degrees C: “); 
  // convert the voltage to temperature in degrees 
  float temperature = (voltage - .5) * 100; 
  Serial.println(temperature);
  if(temperature < baselineTemp){ 
  digitalWrite(2, LOW);
  digitalWrite(3, LOW); 
  digitalWrite(4, LOW); 
  }else if(temperature >= baselineTemp+2 && temperature < baselineTemp+4){ digitalWrite(2, HIGH); 
  digitalWrite(3, LOW); 
  digitalWrite(4, LOW);
  }else if(temperature >= baselineTemp+6){ 
  digitalWrite(2, HIGH); 
  digitalWrite(3, HIGH); 
  digitalWrite(4, HIGH);
} 
delay(1); 

}

PreviousThe CO2 and air quality sensorNextExplaining the Code

Last updated 5 years ago

Was this helpful?