Example Code for Arduino-Ambient Light Analysis -UART (Based on ThingSpeak)

Upload the detected light intensity values to ThingSpeak via UART communication using the WiFi IoT module, and generate a line chart to analyze daily light variations. This allows you to identify the periods with the most and least daylight during the day.

Hardware Preparation

  • DFRuinoUNO board x1
  • WiFi IoT module x1
  • Analog Ambient light sensor x1
  • Wires

Software Preparation

Wiring Diagram

Connect the pin D/R, C/T, GND, VCC of the WiFi IoT module to the Pin D10, D11, GND and VCC of the Arduino IDE. The analog ambient light sensor goes to A0. You can also defin the related pins in programming.

How to use ThingSpeak

Open ThingSpeak website,, click the right-upper corner to enter the register and login interface.

7-1

When using for first time, register an account in the following interface. Fill in the correct email address and vertify the mail box.

7-2

7-3

The following interface will appear after finishing the registeration.

7-4

The above interface can also be found by clicking "Channels->My Channel". Now click new channel.

7-5

Enter the interface below then.

7-6

Here, we need to fill in the name, channel description, and tick the number of fields. In the makecode library, the fields are the same as the fields in the channel. In the routine, only the noise analog value is transferred to the channel. So only one is ticked here, and the field name can be customized. We modify the field name here as noise. Save the channel now. Here is the channel I created.

7-7

After saving, the channel is created. The created channel is shown in the figure below, the parameters uploaded can be seen here.

7-8

If more data needs to be uploaded, you need to click channel settings, click to enter the figure below, tick the "box" behind the field, and save it. You can also delete channel and clear channel data here.

7-9

Channel added successfully.

7-10

Click API Keys then we can see the related password of the channel. The password in the red box should be filled in the corresponding block in programming.

7-11

Sample Code

#include "DFRobot_WiFi_IoT_Module.h"
    #include <SoftwareSerial.h>

    //UART mode
    //Use software serialport RX:10,TX:11
    SoftwareSerial    mySerial(10, 11);
    DFRobot_WiFi_IoT_Module_UART IoT (&mySerial);

    //Configure WiFi
    const char *WIFI_SSID             = "hitest";   //WiFi Name 
    const char *WIFI_PASSWORD         = "12345678";  //WiFi Password

    //Configure Thingspeak
    const char *ThingSpeak_ADDRESS    = "api.thingspeak.com";   //ThingSpeak address, not revise here 
    const char *THINGSPEAK_KEY        = "4I1G5SXC5PGU3HA4";  //Fill in the created event password on ThingSpeak  

    //Send Message to ThingSpeak
    const char *THINGSPEAK_VALUE_1    = "Value1";
    const char *THINGSPEAK_VALUE_2    = "Value2";
    const char *THINGSPEAK_VALUE_3    = "Value3";

    /*******************************************************
    Function:     getAmbient
    Description:  Get light sensor value 
    Params:
    Return:       current light value 
    ******************************************************/
    int getAmbient()
    {
      int val;
      val = analogRead(0);
      return val;
    }

    void setup() {
      //Use software serialport 9600 as communication port
      mySerial.begin(9600);

      //Use serialport 115200 as print port 
      Serial.begin(115200);

      //Init communication port 
      while(IoT.begin() != 0){  
        Serial.println("init ERROR!!!!");
        delay(100);
      }
      Serial.println("init Success");

      //Connect to WiFi
      while(IoT.connectWifi(WIFI_SSID, WIFI_PASSWORD) != 0){  
        Serial.print(".");
        delay(100);
      }
      Serial.println("Wifi Connect Success");

      while(IoT.HTTPBegin(ThingSpeak_ADDRESS) != 0){
        Serial.println("Parameter is empty!");
        delay(100);
      }
      Serial.println("HTTP Configuration Success");

      while(IoT.thingSpeakBegin(THINGSPEAK_KEY) != 0){
        Serial.print("Parameter is empty!");
        delay(100);
      }
      Serial.println("ThingSpeak Configuration Success");
    }

    void loop() {
      String data =(String)getAmbient();         
    //Upload current ambient light data to ThingSpeak every 1s
      IoT.thingSpeakSendMessage(data.c_str());  
      delay(1000);
    }

Result

Upload the data detected by the analog ambient light sensor to ThingSpeak, and display them in an line chart.

1-1

Was this article helpful?

TOP