2. Writing and uploading firmware to EMIT (Arduino) - ControlBits/EMIT GitHub Wiki

2.1 Structuring our Arduino C++ code

OK, so we are just about ready to start programming EMIT but before we write,compile and upload our first code, it's worth having a brief discussion about structuring our Arduino C++ code.

Unlike native C++ programing language that has the famous main() function, an Arduino sketch consists of two main functions; Setup() that contains the initialization code that is exceuted once and the loop() function that contains the code we want to run continuously.

upload

2.2 Useful Arduino resources

The last thing to share before we get started, is the official Arduino document library, this will be an invaluable reference as you develop EMIT's firmware:

OK, let's get started!

2.3 Writing our first code for EMIT

So, finally we're ready to write some (very simple) test code to check that our IDE and EMIT is all set up and ready to go. Over the next few tutorials, we will build several projects as steps to build our complete Environmental Monitoring Sensor firmware.

In this first example, we're simply going to make the Red LED on EMIT flash and print the code "LED ON" and "LED OFF" to the Serial Terminal in the Arduino IDE. Let's start with creating a new project in Arduino.

Open Arduino IDE, select 'File' -> 'New', this will open a New Window.

Close the previous one and on the new window click CTRL+s or select 'File' -> 'Save' to save the project in the Arduino sketchbook directory.

file_save

We will name the project 'red_blinky'.

save_dialog

By default the IDE will create two main functions setup() and loop():

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

}

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

}

As we said above, we'll do all of the setup in the setup() function. First we need to configure the ESP32's GPIO pin that is connected to the Red LED - in this case it's GPIO 16.

To improve the code readability we will define a constant 'EMIT_RED_LED' for the Red LED assigned to GPIO16. This is done by adding this line before the setup() function:

#define EMIT_RED_LED 16

Then in the setup function we will configure the Red LED GPIO to be an output:

    pinMode(EMIT_RED_LED , OUTPUT);

And write a value of LOW(0) to it to turn the LED off, writing HIGH would turn ON the LED.

    digitalWrite(EMIT_RED_LED , LOW);

As we want to print the LED status to the serial terminal we will initialize the default serial module to a standard baud rate 115200.

    Serial.begin(115200);

IMPORTANT: Don't forget that in C++ each of the statements in the code should be terminated by a semicolon, that might lead to a compilation error if its missing.

Our complete setup() code (with some comments added) should look like this:

void setup() {

  // Enable the serial terminal
  // Useful for debugging messages
  Serial.begin(115200);

  // configure RED LED GPIO to be OUTPUT
  // so that we would control the LED (ON , OFF)
  pinMode(EMIT_RED_LED , OUTPUT);
  
  // on the begining we will turn OFF the RED LED 
  digitalWrite(EMIT_RED_LED , LOW);
}

Hint: To make our code easier to read, we'll include lots of comments. In C++ & Arduino C++, a comment is identified by a couple of forward slash symbols //. Anything after // will be ignored by the Arduino compiler.

As we want our LED to flash ON and OFF continuously, we'll place all of our main application code within the loop() function which is executed continuously.

We'll start code by waiting for 1 second (using the delay() function which is embedded within Standard Arduino libraries) before turning the Red LED ON by assigning the value of 'HIGH' to the EMIT_RED_LED pin.

  // Wait for 1 seconds ( 1000 milliseconds )
  delay(1000);

  // turn the red LED on
  digitalWrite(EMIT_RED_LED , HIGH);

At the same time, we also want to print the message "LED ON" to the Serial monitor, so that we can see that the code is running correctly. This is achieved with the following line of code:

  // Print a debugging message on the Serial Terminal
  Serial.println("LED ON");  

Finally we want to wait for another 1 second, turn the Red LED OFF (by assigning the value of 'LOW' EMIT_RED_LED pin and print the message "LED OFF" to the Serial terminal:

  //Wait for 1 seconds ( 1000 milliseconds )
  delay(1000);
  
  // turn the red LED off
  digitalWrite(EMIT_RED_LED , LOW);

  // Print a debugging message on the Serial Terminal indicating that the led is turned OF
  Serial.println("LED OFF");

Our complete loop() function code should look like this:

void loop() {
  //Wait for 1 seconds ( 1000 milliseconds )
  delay(1000);

  // turn the red LED on
  digitalWrite(EMIT_RED_LED , HIGH);

  // Print a debugging message on the Serial Terminal indicating that the led is turned ON
  Serial.println("LED ON");

  //Wait for 1 seconds ( 1000 milliseconds )
  delay(1000);
  
  // turn the red LED off
  digitalWrite(EMIT_RED_LED , LOW);

  // Print a debugging message on the Serial Terminal indicating that the led is turned OF
  Serial.println("LED OFF");

}

OK, so now we're ready to compile and upload the code we've just written to EMIT using the Arduino IDE and get that LED flashing!

2.4 Uploading our first code to EMIT

First we need to compile our code and if it's compiling successfully we will upload it to the ESP32. note that if we just press the upload button it will recompile the code before uploading it to the ESP32. Click on the Verify button to compile the code and check if there's no error happened.

compile

If it compiles successfully you will see a message 'Done compiling'.

compile_success

Click 'upload' button to upload the code to the ESP32. after following the previous stated steps (1.3) for connecting the board, choosing the board name from 'Tools' -> 'boards' and 'Tools' -> 'port'.

upload

While uploading you need to press and hold the BOOT button on the ESP332 DOIT DEVKIT V1 board. so that the ESP32 would enter the boot mode, you should click the BOOT button when you see the text 'Connecting..............' on the Arduino console. Eventually, the Installing firmware dialog box will confirm that installation has been completed.

Click on the serial monitor to see the debugging messages serial

Make sure the baud rate matches that of the code. All being well .. the Red LED on your EMIT development board should now be flashing and the words "LED ON" and "LED OFF" appearing in the Serial Monitor as shown below. serial_monitor

Congratulations! you're now ready to move on to: 3. Measuring Temperature & Humidity