LamaPLC: Arduino basic

Lamaplc: Arduino basic Arduino is an open-source hardware and software company, project, and user community that designs and manufactures single-board microcontrollers and microcontroller kits for building digital devices. Its hardware products are licensed under a CC BY-SA license, while the software is licensed under the GNU Lesser General Public License (LGPL) or the GNU General Public License (GPL), permitting the manufacture of Arduino boards and software distribution by anyone. Arduino boards are available commercially from the official website or through authorized distributors.

Arduino board designs use a variety of microprocessors and controllers. The boards are equipped with sets of digital and analog input/output (I/O) pins that may be interfaced to various expansion boards ('shields') or breadboards (for prototyping) and other circuits. The boards feature serial communications interfaces, including Universal Serial Bus (USB) on some models, which are also used for loading programs. The microcontrollers can be programmed using the C and C++ programming languages, using a standard API which is also known as the Arduino Programming Language, inspired by the Processing language and used with a modified version of the Processing IDE.

A comparison of the technical data of some Arduino boards

TypechipCPU
speed
Power supplyOperation
voltage
Digital
number of pins
(pwm pins)
Analog
number of pins
Flash
memory
SRAM
memory
EEPROM
size
Extras
Arduino Uno R3ATmega328P16 MHz6..20V5V14 (6)632 KB2 KB1 KB -
UNO WiFi Rev2ATmega480916 MHz6..20 V5V14 (6)648 KB6,144 Bytes256 BytesWIFI; Bluetooth LE
UNO R4 WiFi
(ESP32-S3)
Renesas RA4M1
(Arm Cortex-M4)
48 MHz6..24 V5V14 (6)648 KB256 kB Flash - WIFI; Bluetooth; UART, I²C, SPI, CAN; USB-C port; DAC (12 Bit); OP AMP; LED matrix; HID support
UNO R4 MinimaRenesas RA4M1
(Arm Cortex-M4)
48 MHz6..24 V5V14 (6)6256 kB32 kB8 kBUART, I²C, SPI, CAN; USB-C port; DAC (12 Bit); OP AMP; SWD
UNO R4 WiFiArm Cortex-M448 MHz6..24 V5V14 (6)6256 kB32 kB8 kBUART, I²C, SPI, CAN; USB-C port
built-in Wi-Fi and Bluetooth
on-board 12×8 LED matrix
Arduino Mega2560ATmega256016 MHz6..20V5V54 (15)16256 KB8 KB4 KB -
Arduino Giga R1 WiFiSTM32H747XI
Dual Core (Cortex® M7-Kern)
480 MHz6..24 V3,3 V76 (13)122 MB1 MB-Wi-Fi® 802.11b/g/n 65 Mbps
Bluetooth® Low Energy
Micro UFL connector
4x UART
3x I²C
2x SPI
1x CAN
Arduino DueAT91SAM3X8E
SAM3X8E ARM Cortex-M3
84 MHz9 V DC3,3 V54 (12)12512 KB96 KB-USB
USB OTG
4x UART
1x CAN
Arduino Nano3.x: ATmega328,
2.x: ATmega168
16 MHz7..12V5V14 (6)832 KB
16 KB
2 KB
1 KB
1 KB
512 Byte
Mini-B USB connector
Arduino MicroATmega32u416 MHz7..12V5V20 (7)1232 KB2.5 KB1 KB5 hardware interrupts, full speed USB
Keyes Pro MicroATmega32u48 MHz7..9V3.3V12 (5)432 KB2.5 KB1 KB5 hardware interrupts, full speed USB
Pro Micro 5VATmega32u416 MHz7..9V5V12 (5)432 KB2.5 KB1 KB -
Arduino MiniATmega328P3,3V: 8MHz
5V: 16 MHz
6..20V3,3V
5V
14 (6)832 KB2 KB1 KB2 hardware interrupts, no USB port
STM32F103STM32 32-bit Arm Cortex72 MHz5V2.7V..3.6V161664 KB20 KB - USART / I2C / SPI / USB / CAN / DMA
STM32F401STM32 32-bit Arm Cortex25 MHz5V2.7V..3.6V1616256 KB64 KB - USART / I2C / SPI / USB / CAN / DMA
Wemos D1ESP8266EX-9..12V3.3V11 (11)14 KB2 KB1 KBintegrated ESP8266
DuemilanoveATmega168
ATmega328-
6..20V5V14 (6)616KB
32KB
1KB512 byte
1KB
-
Digispark---5V141016KB2KB1KB-
RoboRED---5V/3.3V14632KB2KB1KB-
ATmega1280ATmega1280-5V5416128KB8KB4KB-
Arduino LeonardoATmega32u416 MHz5V20 (7)1232KB2.5KB1K-
Arduino Due--3.3V5412512KB96K--
ChipKIT Max32Diligent-3.3V8316512KB128KB--
Arduino MKR ZEROSAMD21 Cortex®-M0+ 32bit low power ARM MCU48 MHz5V3.3V227 input + 1 output256 KB32 KBnoI²S bus & SD for sound, music & digital audio data
Arduino MKR FOX 1200SAMD21 Cortex®-M0+ 32bit low power ARM MCU48 MHz5V3.3V87 input + 1 output256 KB32 KBnoWifi on board: 868 MHz
Sigfox: Smart RF ATA8520

Memory types of Arduino boards

Memory types of Arduino boards

Flash memory

This is considered the main memory of Arduinos, it stores the downloaded program and preserves its content even after switching off, i.e. it is enough to download the program here only once, it restarts itself every time it is switched on again. During programming, we cannot count on the entire memory, as the download program (bootloader) and, for example, various communications also take parts from here.

In addition, the downloaded libraries can also take up a lot of space from here, so even with a 32 kb memory by default, we can count on only 24-30 kb (without libraries).

In addition, the flash cannot be rewritten indefinitely, its maximum number of write cycles was limited to 100,000. This is enough to store a program rewritten 10 times a day for about 27 years without any problems.

SRAM

static random-access memory

Simply put, SRAM stores the internal variables defined in the program. SRAM - in contrast to flash memory - does not retain its contents in a power-free state, therefore, after each power-on, the program redefines the variables and they are transferred to the SRAM with their default values determined there.

EEPROM

Electrically Erasable Programmable Read-Only Memory

EEPROM is the non-volatile variable memory of boards. This - similarly to Flash - preserves its content even when it is turned off, but - and just like Flash - it is only certified for 100,000 write cycles, i.e. it is not really suitable for cyclic data writing, for example. Plus, it's a bit slower to handle than regular SRAM.
Due to these technical characteristics, the EEPROM can be used for the following functions:

  • storage of configuration(s).
  • saving initial settings
  • save counters, values, collected values (e.g. operating hours counter) independent of restarts

Different Arduino boards have different EEPROM sizes:

  • ATmega168: 512 Bytes
  • ATmega8: 512 Bytes
  • ATmega328P: 1024 Byte
  • ATmega32U4: 1024 Bytes
  • ATmega1280: 4096 Bytes
  • ATmega2560: 4096 Bytes

Arduino IDE

Arduino IDE The Arduino IDE is a development system written in JAVA language, with which, after we write programs for the Arduino, it compiles and debugs them, and then downloads them to the cards. The download is most often done via the USB port, which can be found on almost every Arduino board, but it is also possible to download via ISP or OTA, if the given board also provides these options.
Arduinos have a pre-flashed bootloader, which allows downloading of codes without external hardware, simply via the STK500 protocol.

If necessary, the above bootloader can be bypassed using the ICSP (In-Circuit Serial Programming).

Of course, we have many options for programming Arduinos outside of the rather inexpensive (but efficient and free) Arduino IDE. for example, VisualStudio can also be used for this purpose after installing an Arduino plugin.

Arduino OTA

Over the Air
The arduino also gives you the option to download programs via wifi or even an ethernet shield. ESP modules (ESP8266 or ESP32) offer a particularly good opportunity to exploit this function, which can be directly integrated with Arduino boards, and thus most of the implementation of downloading via Wifi is carried out by these modules.

Arduino API

Application Programming Interface
The functionality of Arduino can be extended through the library system. The use of libraries requires memory, but with their careful selection, the memory load can be well optimized, and a significant part of the programming can be saved by using them. There are standard libraries such as Wire, AVR_C, String,.., but there are also a huge number of very well developed libraries available for download from Github. When describing the sensors, I almost always refer to the Github libraries.

Arduino Bootloader

A special program preloaded on Arduino Boards and compatible with the Arduino IDE, which allows programs to be uploaded to boards without special tools; usually via USB.

Sketch

The programs that can be run on Arduino are called sketches. Sketches can be saved in “.ino” files or loaded from there. There are many (basic) example programs in the Arduino IDE under File / Examples, but almost every installed directory also contains example programs (they can also be opened in File / Examples after installation, they are at the bottom of the list).
Many sketches communicate with the computer via a serial connection. The Arduino IDE has a built-in serial monitor or terminal to help display this data. You can also send data to the board using the Monitor. You can find the serial monitor under “Tools” → “Serial monitor”. Starting this usually reboots the Arduino board. Make sure you set the speed, or baud rate, to the correct value. If you don't do that, you'll either see garbage or nothing here. The typical data transfer rates are: 9600 or 115.200 baud.

Arduino and USB

Arduino boards can basically be programmed from your PC via the USB port (see above, Bootloader, but there are quite a few other solutions as well…), and during the program's execution, the serial information can also be sent to your PC via this. . Most boards include the classic FTDI or AtMega16U2 USB communication IC for the connection, but it may happen that the so-called (Chinese) CH340..CH341 ICs are built in. Each of these requires a different driver, this must be taken care of when using a different board for the first time, because it is possible that our PC does not see the Arduino.

Most Arduino USB ports have an overcurrent protection (resettable polyfuse), which automatically releases in the event of a current consumption exceeding 500 mA or a short circuit.

Arduino USB UART converter types:

  • FTDI: Converter type of early Arduinos, proved to be too expensive. It is still used as an external converter (connected to the ISP)
  • Atmega8U2: The first serial Uno-k converter, up to version R2
  • AtMega16U2: Currently most (official) Arduino board UART converters
  • CH340/CH340G/CH341: typical converters of Chinese-made Arduino clones, downloading the drivers is somewhat difficult

TTL logic levels

The (digital) electronic equipment that uses logical high (HIGH) and low (LOW) levels are mostly the so-called The voltage levels connected to high/low signals are defined based on TTL logic (Transistor-Transistor Logic). The Arduino logic levels differ slightly from these levels:

Arduino TTL logic levels

  • VOH: Minimum output supply voltage level. Above that, the TTL device provides a HIGH signal
  • VIH: Minimum input voltage level for the HIGH signal
  • VIL: Maximum input voltage level for low (LOW) signal
  • VOL: Maximum output voltage for a low (LOW) signal

The evaluation of the input (HIGH or LOW) becomes uncertain on the covered parts. While the program logic levels can be characterized by the constants true (true, 1) and false (false, 0), the state of the pins can be characterized by the constants HIGH (high, 1) and LOW (low, 0).

The HIGH state occurs when a signal is read above 3V signal level on the 5V inputs, while above 2.0V on the 3.3V inputs. The LOW states are under 1.5V for 5 boards, approx. for 3.3V boards. They appear below 1.0V.

Intermediate voltage levels (1.5V - 3.0V for 5V boards, 1.0V - 2.0V for 3.3V boards) should be avoided because the input state becomes uncertain.

If the input pull-up resistor was previously activated on the given pin, there is a good chance that we will never read a HIGH state from it, see: digitalRead().

In the case of outputs, after releasing the HIGH state, the pin takes the maximum voltage level, i.e. 3.3V for 3.3V boards, 5V for 5V boards.

If the internal pull-up resistor was previously activated, the output voltage will also be less than 5V (approx. 3.3V) in case of a HIGH signal, see: digitalWrite().

Load capacity of pins

Arduinos (due to their simple construction) cannot really be loaded, and this must be taken into account for all controls that require more current (solenoid valves, relays,…). In the case of a more serious overload, the board simply dies, and in the case of a smaller (and short-term) overload, it switches off. In the case of poorly dimensioned relay control, it may happen that the Arduino simply turns it on and off (the relays click accordingly) - well, in such a case, it is worth considering the load on the board.

Main load capacity limits (of course these may vary by type):

  • Pin load capacity for UNO (5V): 20 mA
  • Pin load capacity for the Mega board (5V): 40 mA
  • Pin load capacity (for 3.3V): 10 mA
  • Pin load capacity for Giga R1 WiFi (for 3.3V): 8 mA
  • Maximum load capacity (UNO) of all pins (Vcc, GND): 200 mA
  • Maximum load capacity (Mega) of all pins (Vcc, GND): 400..800 mA

Use of pins

There are a few tips and tricks for using the Arduino pins, which are also the causes of many inexplicable problems:
Pin0 and pin1 are connected to serial communication, so if we want to receive information from the board and use the Serial.print instruction to send information to the serial monitor, these pins cannot be used as digital input/output at this time. If possible, we should not even define these as digital channels, or if this is not possible because we have run out of pins, then we should pack a function here that will make us say goodbye to the use of serial for the time being.

The analog pins can be used as digital ports without further ado, in which case you simply refer to them as pins 14..19, for example, in the case of the Uno, as shown in the figure below. My own experience is that it is worthwhile to designate the ports to be left free already during the design. Since I mostly use I²C for communication, I use A4 and A5 for this communication in the case of the Uno. In the same way - especially for testing - it is worth considering the serial communication and the corresponding ports, i.e. the 0 (Rx) and 1 (Tx) pins in the case of the Uno.
Arduino, Use of pins

Arduino ISP/ICSP

The ISP (In-system programming) port, which is typically integrated on Arduino (and compatible) boards, provides the possibility to download a program to the board by bypassing USB/bootload. It also provides communication options for SPI communication, for example between cards. Pin assignments of possible ports:

Arduino ISP/ICSP

In addition, these pins are also connected in parallel with the corresponding pins of the IO ports (see below), so they are simply grouped here for downloads. It is also possible to use an Arduino board to upload programs to other Arduinos via ISP.

Arduino ISP portnameshort description
MISOMaster In Slave Outinput as master, output as slave
VTG5Vinput 5V+
SCKSerial Clockserial clock signal, provided by the master for SPI
MOSIMaster Out Slave Inoutput as master, input as slave
RSTResetreset
GND0V0V

Arduino ISR

The ISR (interrupt service routine) function was developed to monitor inputs with rapidly changing states. These interruptions are typically suitable for counting or monitoring fast pulses (Hall pulses - flowmeters, current signal pulses) and are independent of the program run cycle.
From the software side, these signals must be handled with the attachInterrupt() function, and these fast signals can only be read on the ports designated for this purpose:

BoardDigital ports assigned to interrupt
Arduino Uno, Arduino Nano, Mini, other 328-based2, 3
Arduino Mega, Mega2560, MegaADK2, 3, 18, 19, 20, 21
Micro, Leonardo, other 32u4-based0, 1, 2, 3, 7
Wemos D1all digital outputs (except D0)

Arduino PWM

PWM (pulse-width modulation) is a form of signal generation that can be used to create analog output signals on the digital output, i.e. it is a digital-to-analog converter function. PWM outputs can be controlled with the analogWrite() function on Arduinos. The PWM function is available on the designated - marked with ~ - PWM digital outputs.

Boarddesignated PWM outputs
For most ATmega168 or ATmega328 boards
Arduino Uno, Arduino Nano*
3, 5 , 6, 9, 10, 11 pin, pin 5-6: 980 Hz, the rest: 490 Hz
Arduino Mega2..13. and 44..46. pin
Wemos D1all digital outputs (except D0)
Arduino Micro0, 1, 2, 3, 7
for older boardspin 9, 10 and 11

*: for ATmega168 or ATmega328 boards:

  • timer0: pin 5 and 6, 8-bit PWM
  • timer1: pin 9 and 10, 16-bit PWM
  • timer2: pin 11 and 3, 8-bit PWM

For servos that require 16-bit resolution, it may happen that only pins 9 and 10 will work!

Only 8-bit PWM can be implemented with the Arduino Micro.

Arduino AREF pin

Via the AREF pin, the analogReference() function can be used to set the external (type: EXTERNAL) reference voltage (i.e. the maximum value of the input range) used for the analog input.

Arduino communication

Comparison of the most frequently used direct* communication solutions for Arduino systems:

*: Ethernet, Wifi, Can,.. are not direct communication in this sense, because they require some kind of converter, and the converters are only available with the communication forms below.

communication solutionSerial (UART)I²C1-WireSPI
maximum number of partners / slaves P2P communication,
1 master + 1 slave
theoretically 128 stations no theoretical limit,
practically approx. 150
Defined by the number of SS pins.
without SS 1 master and 1 slave
bridgeable distance (≈)9600 baud: 2-3 m
300 baud: >100 m (twisted pair)
100 Kbaud: 1 m
10 Kbaud: 6-8 m
with 20 sensors max. 100 mmax. 5 m
number of pins used2: Tx, Rx2: SDA, SCL1: DATA3 or 4: SCK, MOSI, MISO, (SS)
restrictionsUsing pins 0 and 1 for serial download and serial monitor does not workBlocks the following pins: SDA-A4, SCL-A5-Blocks 4 pins

Arduino serial communication

Arduino hardware serial communication

The default communication of Arduino boards is serial (UART), which is carried out via USB with the programming device or the communication partner PC. The TX and RX pins of the boards are also connected in parallel with the USB port, so if you use serial communication, you cannot use these pins as digital ports.

This channel can of course also be used for other UART communication, for example RS-232, but in this case you have to pay attention to the voltage levels, which in the case of RS-232 is typically ± 12V. All Arduino boards have at least one serial (UART) port, their pin assignment:

boardserial communication pins
Arduino Uno, NanoRx←0, Tx→1
Arduino Mega, DueSerial1: pin 19 (RX) pin 18 (TX), Serial2: pin 17 (RX) pin 16 (TX), Serial3: pin 15 (RX ) pin 14 (TX)

Arduino software serial communication

Software serial communication is made possible by using the SoftwareSerial library. While HW serial communication uses HW components for the UART and it works while performing other tasks (as long as there is room in the 64 byte buffer), software serial communication is done from software. This is obviously at the expense of the execution of the other software, but it enables multiple serial communication at the same time, at a speed of up to 115.200.

Limitations of software serial communication
  • If several software serial ports are to be used, only one can receive data at the same time
  • Not all pins of Mega and Mega 2560 are suitable for software serial communication, the following pins can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 ( 63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).
  • For Leonardo, only the following pins can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
  • On the Arduino Genuino 101, the maximum RX speed can be 57,600 bps, and pin 13 cannot be used as RX.
Data loss during software serial communication

When you send or receive long messages using software serial communication, they may arrive with some characters missing. The reason for this is not necessarily to be found in the code. Maybe the SoftwareSerial receive buffer gets full and then discards the characters.

The easiest solution to this is to increase the software's serial buffer from the default 64 bytes to 256 bytes (or just larger than 64 bytes):

On the PC, open the following file: C:\Program Files (x86) → Arduino → hardware → Arduino → avr → libraries → SoftwareSerial → SoftwareSerial.h and change the following line:

// RX buffer size
#define _SS_MAX_RX_BUFF 64 // change 64 to max 256 for example

Arduino I²C

One of the most commonly used communications on Arduino boards is I²C. The default ports for communication on the various boards are as follows:

BoardSDA (data port)SCL (clock port)note
Arduino UnoA4A5
Arduino Mega2021
Arduino Nano45
Wemos D1A4 (SDA)A5 (SCL)all digital outputs
(except D0) can be applied to I²C
NodeMCU (ESP8266)D2(GPIO4)D1(GPIO5)D0 cannot be used for I²C

Arduino SPI

One of the commonly used communications on Arduino boards is SPI. On Arduinos, the ISP connection module also uses SPI, which is relatively easy to project (unfortunately, SS did not have a place here):

Arduino SPI

BoardSPI SS
(select)
SPI MOSISPI MISOSPI SCK
Arduino UNO10111213
Arduino Mega53505152
Arduino NanoD10D11D12D13
NodeMCU (ESP8266)D8(GPIO15)D7(GPIO13)D6(GPIO12)D5(GPIO14)

Arduino 1-wire

The 1-wire bus describes a Dallas Semiconductor Corp. serial interface that handles a data line (DQ) that is used as both a power supply and a transmit and receive line. The term 1-wire is misleading because communication also requires ground (GND). In fact, even with 1-wire, we always use at least two physical wires (GND, DQ).

Arduino Ethernet

Arduino is of course suitable for communication via ethernet and access to Internet functions. The easiest way to access ethernet is to use an ehternet module or extension. Modules and add-ons typically communicate with Arduino boards via SPI:

Arduino Uno Ethernet SPI pin assignmentArduino Mega Ethernet SPI pin assignment
Arduino Uno Ethernet SPI PinoutArduino Mega Ethernet SPI Pinout

A micro-SD card reader is also integrated on most ethernet boards. The use of this is optional, but it occupies an extra pin on the board.

The Ethernet communication of modules / extensions is typically carried out by WIZnet W5x00 series ICs. A comparison of their main features:

FeatureW5100W5300W5500
Interface Modedirect, indirect, SPIdirect, indirectSPI
Number of sockets448
Speed (max, MBPS)252515
Data busOnly 8 Bit, DATA[7:0]16/8 Bit, DATA[15:8]/DATA[7:0]16/8 Bit
Title bus15 PINs, ADDR[14:0]10PINs, ADDR[9:0]10PINs, ADDR[9:0]
Memory size(Fixed) 16KBytes
TX : 8KBytes, RX : 8Kbytes
(Configurable) 128KBytes
TX : 0~128KBytes, RX : 0~128KBytes
(Fixed) 32KBytes
TX : 16KBytes, RX : 16Kbytes


This page has been accessed for: Today: 1, Until now: 245