Skip to content

Latest commit

 

History

History
124 lines (77 loc) · 3.62 KB

File metadata and controls

124 lines (77 loc) · 3.62 KB

Arduino

Development with the help of the Arduino IDE:

Select microcontroller

Boards Manager

The Arduino IDE natively supports Arduino board, which is to be expected ;-)

In order to select the right ESP board, they first have to be installed. The Boards Manager needs to know where to get that information.

First open up the preferences/settings dialog by pressing ⌘ , (Command comma, or Control comma on Windows)

Arduino IDE preferences dialog

and put the following in the Additional Boards manager URLs field

https://dl.espressif.com/dl/package_esp32_index.json
https://arduino.esp8266.com/stable/package_esp8266com_index.json

Then head over to the boards manager itself, filter on esp and click the 'Install' button.

Arduino IDE boards manager

This will not only add the ESP boards to the list, but also installs the tool chain needed.

Note that on MacOS you may need to install the Xcode Command Line Tools

xcode-select --install

Drivers

On MacOS, connecting to a microcontroller either directly via USB or via an FTDI USB to Serial converter, will probably work out of the box. For Windows some drivers might be required. Some download links:

TODO?? Explain UART and FDTI

Write code

This piece of code below continuously adds the number 2 and 3, but that's it. No input, no output.

What it does do, is demonstrate the structure of a microcontroller sketch.

/*
    This is a not so useful sketch
*/
#include <Arduino.h>

#define NUMBER_ONE 2

const int numberTwo = 3;

int result;

// put function declarations here:
int myFunction(int, int);

void setup()
{
    // put your setup code here, to run once:
    int result = myFunction(NUMBER_ONE, numberTwo);
}

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

// put function definitions here:
int myFunction(int x, int y)
{
    return x + y;
}

Comments are either preceeded by // or wrapped inside /* ... */ and can clarify what's going on in the code.

#include pulls in constants and function definitions from libraries that are needed. In this case the 'core' Arduino library.

After that define global constants and variables. Constants cannot be changed during program execution, regular variable can.

Then put the so-called forward declaration of your functions. This is good practice, but not neccessary. The compiler looks at the sketch as a whole and figures out what goes where.

Just remember: A function can be

  • declared
  • defined
  • called (or invoked)

In this case myFunction() is declared on top, defined at the botton and called from setup() in the middle.

Two special reserved functions need to exist in an Arduino sketch: setup() and loop(). The first one is executed once and typically used to initialise the runtime enviroment. The second one is executed repeatedly and never stops (unless the power is unplugged).

See also: https://docs.arduino.cc/learn/programming/sketches/

Compile code

Deploy to microcontroller

Monitor output