Arduino-Basics(1)
Arduino-Basics(1)
Introduction to
Arduino programming
Marko Simeunović, PhD
Introduction
Arduino – today most popular platform for rapid
prototyping.
Consists of:
microcontroller board (hardware) and
IDE - integrated development environment (software).
Excellent for starting to learn electronics.
Its popularity comes from:
built in programmer,
microcontroller functions are organized in accessible
packages and
programming is done using high level C++ language.
Introduction
Suitable for both beginners and professionals.
Advantages:
Inexpensive,
Cross-platform-based,
Simple and clear programming environment,
Open source and extensible software,
Open source and extensible hardware.
Large community.
How to start?
Arduino official page – https://www.arduino.cc/
https://playground.arduino.cc/
https://www.instructables.com/howto/Arduino/
https://itp.nyu.edu/physcomp/
https://makezine.com/category/technology/arduino/
Arduino boards
ATmega328P ATmega32U4 AT91SAM3X8E
3€ 1.5€ 12€
ATmega2560 ATmega168V
ATmega328
6€ 2€ 2€
Arduino shields
3€ Ethernet WiFi
shield shield
Xbee
shield
Sensors
Arduino Uno
Pins:
GND
5V and 3.3V
Analog
Digital
PWM
AREF
Communication:
UART
I2C
SPI
NodeMCU
Setup Arduino IDE
http://arduino.esp8266.com/stable/package_esp8266com_index.json
Setup Arduino IDE
Tools/Board/Board Manager
Setup Arduino IDE
Program structure
void setup()
{
}
void loop()
{
// Infinite loop
}
Reading and writing from Serial
Serial.begin(baud) - sets the data rate in bits per
second (baud) for serial data transmission.
Data rates: 300, 600, 1200, 2400, 4800, 9600, 14400,
19200, 28800, 38400, 57600, or 115200.
Serial.read() – reads the first byte of incoming
serial data available. It returns -1 if there is no data.
Serial.readString() – reads string.
Serial.write(data) – writes data to the serial port.
Serial.available() - returns the number of bytes
(characters) available for reading from the serial
port.
Reading and writing from Serial
char input;
void setup() {
Serial.begin(9600);
delay(2000);
Serial.println("Type something!");
}
void loop() {
if(Serial.available()){
input = Serial.read();
Serial.print("You typed: " );
Serial.println(input);
}
}
Software Serial
Enables using several Serial ports on Arduino based
board.
In order to use it, a library has to be included:
#include <SoftwareSerial.h>
Creating SofrwareSerial object on D1 and D2 pins:
SoftwareSerial EEBlue(D1, D2); // RX | TX
Limitations:
If using multiple software serial ports, only one can receive
data at a time.
Not supported by some pins on some boards.
Limitations on maximum speed on RX pin.
NodeMCU and HC-05
NodeMCU and HC-05
#include <SoftwareSerial.h>
SoftwareSerial EEBlue(D1, D2); // RX | TX
void setup()
{
Serial.begin(9600);
EEBlue.begin(9600);
Serial.println("We have started...");
}
void loop()
{
// Feed any data from bluetooth to Terminal.
if (EEBlue.available()) Serial.write(EEBlue.read());
void loop()
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
int addr = 0; //
Starting address
void setup()
{
EEPROM.begin(512);
EEPROM.write(addr, 'A');
addr++;
EEPROM.write(addr, 'B');
addr++;
EEPROM.write(addr, 'C');
addr++;
void loop()
{
delay(10);
}
EEPROM read example
#include <EEPROM.h>
int addr = 0;
void setup()
{
EEPROM.begin(512);
Serial.begin(9600);
Serial.println("");
Serial.print(char(EEPROM.read(addr))); addr++;
Serial.print(char(EEPROM.read(addr))); addr++;
Serial.println(char(EEPROM.read(addr))); addr++;
Serial.println(www);
}
EEPROM read example
String readStringEEPROM(int addr, int numByte)
{
String s = "";
for(int i=0;i<numByte;i++)
{
s = s + char(EEPROM.read(addr+i));
}
return s;
}
void loop()
{
delay(10);
}
NodeMCU and WiFi
Connecting to a network:
WiFi.begin(ssid, password);
Retrieve network status:
WiFi.status() – returns WL_CONNECTED if is connected
Getting device IP:
WiFi.localIP();
Setting static IP:
IPAddress ip(192,168,1,200);
IPAddress gateway(192,168,1,254);
IPAddress subnet(255,255,255,0);
WiFi.config(ip, gateway, subnet);
NodeMCU and WiFi
Setting WiFi mode:
WiFi.mode(mode);
mode can be: WIFI_STA and WIFI_AP.
Sending HTTP GET request
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
void setup ()
{
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print("Connecting..");
}
}
Sending HTTP GET request
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("http://jsonplaceholder.typicode.com/users/1");
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println(payload);
}
http.end(); //Close connection
}
delay(30000); //Send a request every 30 seconds
}
HTTP WiFi server on AP
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include "DHT.h"
#define DHTPIN 2
webServer.begin();
dht.begin();
Serial.begin(9600);
Serial.println("Started...");
}
HTTP WiFi server on AP
void loop(void){
dnsServer.processNextRequest();
webServer.handleClient();
delay(2000);
}
Conclusion
A short overview of Arduino programming is given.
We covered the following:
Serial communication,
I/O pin manipulations,
EEPROM manipulation,
Sending HTTP requests,
Setting HTTP server on AP.
This lecture covered only a small part of
functionalities.