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. Controlling LEDs with an Arduino

The Code

The default code when you startup the Arduino IDE looks like this:

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

}

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

}

Include the code to control the LEDs. Start by defining variables so that we can address the lights by name rather than a number.

int redLED = 10;
int yellowLED = 9;
int greenLED = 8;

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

}

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

}

Next, let’s add the setup function, where’ll we configure the red, yellow and green LEDs to be outputs. Since you have created variables to represent the pin numbers, you can now refer to the pins by name instead.

int redLED = 10;
int yellowLED = 9;
int greenLED = 8;

void setup() {
  // put your setup code here, to run once:
  
  pinMode(redLED, OUTPUT);
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);

}

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

}

Finally, write the code that will turn the LEDs on for a certain period of time and then switch them off for another certain period of time:

int redLED = 10;
int yellowLED = 9;
int greenLED = 8;

void setup() {
  // put your setup code here, to run once:
  
  pinMode(redLED, OUTPUT);
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  
  // green off, yellow on for 3 seconds
  
  digitalWrite(greenLED, LOW);
  digitalWrite(yellowLED, HIGH);
  delay(3000);
  
  // turn off yellow, then turn red on for 5 seconds
  
  digitalWrite(yellow, LOW);
  digitalWrite(red, HIGH);
  delay(5000);

  // red and yellow on for 2 seconds (red is already on though)
  
  digitalWrite(yellow, HIGH);
  delay(2000);
  
  // turn off red and yellow, then turn on green
  digitalWrite(yellow, LOW);
  digitalWrite(red, LOW);
  digitalWrite(green, HIGH);
  delay(3000);    

}

Verify and upload the code to your Arduino board using the icons shown below:

PreviousControlling LEDs with an ArduinoNextAdding a button to control the LEDs

Last updated 5 years ago

Was this helpful?

Verify/Upload code (Core Electronics, Australia)