# Arduino

## Arduino

We'll be using the Arduino platform and IDE to write our code

### Install Arduino IDE

Download and install the Arduino IDE.

{% tabs %}
{% tab title="Windows" %}
Download **Arduino IDE** for&#x20;

* [Windows](https://www.arduino.cc/download_handler.php?f=/arduino-1.8.9-windows.exe)
  {% endtab %}

{% tab title="Linux" %}
Download **Arduino IDE** for

* [Linux 32 bits](https://www.arduino.cc/download_handler.php?f=/arduino-1.8.9-linux32.tar.xz)
* [Linux 64 bits](https://www.arduino.cc/download_handler.php?f=/arduino-1.8.9-linux64.tar.xz)
* [Linux ARM 32 bits](https://www.arduino.cc/download_handler.php?f=/arduino-1.8.9-linuxarm.tar.xz)
* [Linux 64 bits](https://www.arduino.cc/download_handler.php?f=/arduino-1.8.9-linuxaarch64.tar.xz)
  {% endtab %}

{% tab title="Mac OS X" %}
Download **Arduino IDE** for&#x20;

* [Mac OS X](https://www.arduino.cc/download_handler.php?f=/arduino-1.8.9-macosx.zip)
  {% endtab %}
  {% endtabs %}

### Install board definitions

Copy this URL

```
http://arduino.esp8266.com/stable/package_esp8266com_index.json
```

1. Open the Arduino IDE preferences
2. In the `Additional Boards Manager URLs`

![](https://3122883480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdM1tLruDjxv-Yy8x7T%2F-LdM4gpYsjMB_Z4SGxFF%2F-LdM4hmJlkYhTfWpD3io%2Farduino-boards.png?generation=1556239279439598\&alt=media)

#### Install the esp8266 boards definitions

Open the Board Manager in **Tools › Board › Boards Manager**

![](https://3122883480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdM1tLruDjxv-Yy8x7T%2F-LdPOw3RflLE7aXp8bm8%2F-LdPPBgaTZz6yDJPwOY9%2Fimage.png?alt=media\&token=9ab2d536-0b0f-4f5d-bd05-5e325ee491d8)

1. Search for **esp8266**
2. Install the boards definition
3. Close Arduino and open it again so the board configurations are loaded

#### Configure Arduino for this project

Connect your board using the USB cable.

* Make sure you are using the appropriate COM port under **Tools › Port**
* Make sure you have selected the appropriate board board under **Tools › Board:**

{% tabs %}
{% tab title="LOLIN D1 mini pro" %}
Use **LOLIN (WEMOS) D1 mini Pro**

![LOLIN (WEMOS) D1 mini Pro](https://3122883480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdM1tLruDjxv-Yy8x7T%2F-Ltm7MLJ71zincbLUx4R%2F-Ltm9QcPKFSE22i23qfe%2Fimage.png?alt=media\&token=4e0c8d68-7857-4ab0-86c6-05aa75306e53)
{% endtab %}

{% tab title="WeMos D1 mini" %}
Use **WeMos D1 R1**

![WeMos D1 R1](https://3122883480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdM1tLruDjxv-Yy8x7T%2F-Ltm7MLJ71zincbLUx4R%2F-Ltm8QuzaQY9wczXLEfy%2Fimage.png?alt=media\&token=8404459b-3211-4787-b257-b7e21bcddd44)
{% endtab %}

{% tab title="Compatible D1 mini" %}
Use **WeMos D1 R1**

![WeMos D1 R1](https://3122883480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdM1tLruDjxv-Yy8x7T%2F-Ltm7MLJ71zincbLUx4R%2F-Ltm8QuzaQY9wczXLEfy%2Fimage.png?alt=media\&token=8404459b-3211-4787-b257-b7e21bcddd44)
{% endtab %}
{% endtabs %}

### Install libraries

#### AdaFruit Neopixel

We are going to use the Library Manager to install this library

![Arduino IDE Library Manager](https://3122883480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdM1tLruDjxv-Yy8x7T%2F-LdOJPJ7JHiLYwJ25rFx%2F-LdOKQyPtF8HUqyR1se3%2Fimage.png?alt=media\&token=727f7486-65c6-4cad-b99a-574c53cb5399)

1. Open the Library Manager by clicking on the menu **Sketch** › **Include Library** ›&#x20;

   **Manage Libraries**
2. Search for neopixel
3. Select **Adafruit NeoPixel** by **Adafruit**
4. Click the **Install button**
5. Don't close the Library Manager dialog yet

#### ArduinoJson

We will use the Library Manager again to install this one

![](https://3122883480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdM1tLruDjxv-Yy8x7T%2F-LdOJPJ7JHiLYwJ25rFx%2F-LdOL6IB-qOP8gEBIvLM%2Fimage.png?alt=media\&token=93a61fb6-3873-4ac2-8305-9dfb7ae30dbf)

1. Open the Library Manager by clicking on the menu **Sketch › Include Library › Manage Libraries**
2. Search for ArduinoJson
3. Select **ArduinoJson** by **Benoit Blanchon**
4. **Make sure you select the v5 version**
5. Click the **Install button**
6. Don't close the Library Manager dialog yet

#### Firebase ESP8266 Client

We will also install this library using the Library Manager

![](https://3122883480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdM1tLruDjxv-Yy8x7T%2F-LtmHn_VDYQxbfb_02_a%2F-LtmMJmocQ3pJotfv_lo%2Fimage.png?alt=media\&token=83de3506-4014-4b9e-b498-fa79345d15cd)

1. Open the Library Manager by clicking on the menu **Sketch › Include Library › Manage Libraries**
2. Search for firebase
3. Select **FirebaseESP8266 Client** by **Mobitz**
4. Click the **Install button**
5. Close the Library Manager

#### Restart Arduino IDE

{% hint style="warning" %}

After updating or installing libraries, restarting Arduino IDE is required for changes to take effect
{% endhint %}

### Test program

Let's write a program that tests our RGB LED and its connections.\
First, open the Arduino  IDE

![](https://3122883480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdM1tLruDjxv-Yy8x7T%2F-LdPEln7cFFkZGALDWMx%2F-LdPF-v6eCxx3lbT2Q4P%2Fimage.png?alt=media\&token=dda47187-fe5d-4356-a90b-a21246160293)

Notice that Arduino programs have at least two functions:

* **Setup** runs once when the device starts
* **Loop** runs again and again, indefinitely, while the device is powered on.

Let's copy the code below and make some magic happen

{% code title="neopixel-test.ino" %}

```c
#include <Adafruit_NeoPixel.h>

// This code asumes your data pin is 2. Change it if you need to
Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, D4, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.setBrightness(255);
  Serial.begin(9600);
}

void loop() {
    strip.setPixelColor(0, strip.Color(255, 0, 0));
    Serial.println("red");
    strip.show();
    delay(500);
    
    strip.setPixelColor(0, strip.Color(0, 255, 0));
    Serial.println("green");
    strip.show();
    delay(500);
    
    strip.setPixelColor(0, strip.Color(00, 0, 255));
    Serial.println("blue");
    strip.show();
    delay(500);
}
```

{% endcode %}

#### Flash your program

* Click the **upload** button on the top bar of the Arduino IDE
* Enjoy your blinking RGB led!

![The upload button](https://3122883480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdM1tLruDjxv-Yy8x7T%2F-Ltm9knxGKW1qD4c8TC7%2F-LtmA6s54qFoKnzE3dEw%2Fimage.png?alt=media\&token=7b7d64f0-bfe3-4ca3-bdfc-23a6640a920b)

### Debugging

#### The serial monitor

Let's take a look under the hood. Open the Serial Monitor on **Tools › Serial Monitor**

{% hint style="warning" %}
Make sure the baud rate on this window matches the one in the code
{% endhint %}

Watch as your code runs

![](https://3122883480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdM1tLruDjxv-Yy8x7T%2F-LdPHZu4BHVGtXDIT6l0%2F-LdPHaIx4_jl3v3vAGrm%2Fimage.png?alt=media\&token=a3da93a0-cfa9-4b16-935c-09be7559c29e)

## References

* <https://github.com/esp8266/Arduino>
* <https://github.com/FirebaseExtended/firebase-arduino>
