Table of Contents
How To Program Arduino Device With C++ ? What is Arduino ?
How To Program Arduino With C++?
Developers can develop apps for the Arduino via Arduino’s own IDE easily. The developer tells the Arduino board what to do by sending a set of instructions written in a subset of C/C++ functions, to the microcontroller on the board. While it uses the C/C++ language, because of its different main program structure and because of its specific commands for its board, it is called as the Arduino programming language (based on Wiring), and the Arduino Software (IDE), based on Processing. List of Arduino boards and compatible systems can be found here. https://en.wikipedia.org/wiki/List_of_Arduino_boards_and_compatible_systems
How To Use Sketch To Program Arduino With C++?
The Arduino IDE refers to its Arduino version C++ programs as a “Sketch” and collectively refers to the source code files as “Sketch files”.
The Arduino programming language can be divided in three main parts: functions, values (variables and constants), and structure. The complete Language Reference can be found here: https://www.arduino.cc/reference/en/
How To Use Arduino IDE to program Arduino With C++?
Ardunio IDE is a kind of text editor or code editor which supports C/C++ functions. The main difference of the Arduino C++ version is it has setup()
and loop()
functions instead of a main()
function in standard C/C++. and it has specific definitions and commands for the ports of Arduino and for its CPU. Mainly it has two different IDE versions 1.x version and new 2.0 release to candidate version.
Arduino IDE 1.x.x (1.8.19) : The open-source Arduino Software (IDE) makes it easy to write code and upload it to an Arduino compatible board. This software can be used with any Arduino board. Refer to the Getting Started page for Installation instructions. Active development of the Arduino software is hosted by GitHub. See the instructions for building the code. The very latest release source code archives are available here.
Arduino IDE 2.x.x RC(2.0.0-rc9) : The new major release of the Arduino IDE is faster and even more powerful. In addition to a more modern editor and a more responsive interface it features autocompletion, code navigation, and even a live debugger. The Arduino IDE 2.0 is now moving to stable status with a series of Release Candidate (RC) builds.
How to program Arduino with C++ or C?
The Arduino uses a version of the language that all standard C and C++ constructs supported by avr-g++ should work in Arduino. The Arduino language is merely a subset of C/C++ functions that can be called from your code. Your code (called as sketch) undergoes minor changes (e.g., automatic generation of function prototypes) and then is passed directly to a C/C++ compiler (avr-g++). The main difference is it has a setup()
function which initializes the device into a ready state. After this a loop()
function acts as the central processing controller instead of a main()
function that we would find in regular C and C++. The setup() and loop() functions look just like regular C/C++ except it has a number of specific definitions/constants and commands for the ports of Arduino and for its CPU. For more details, see the page on the Arduino build process.
Here is the main program structure of the Arduino app. It has two main functions, setup and loop, and loop is like a main function.
1 2 3 4 5 6 7 |
void setup() { } void loop() { } |
How to program Arduino with a regular C++ IDE?
You do not have to use the official Arduino IDE to program Arduino compatible devices. It is possible to use the Eclipse IDE or Visual Studio Code to write programs for Arduino.
Arduino official web page includes a lot of examples, one of the simplest example is a blink example that turns on and off a led on this device. Here is the simplified blink example,
1 2 3 4 5 6 7 8 9 10 11 12 13 |
void setup() { pinMode( LED_BUILTIN, OUTPUT ); } void loop() { digitalWrite( LED_BUILTIN, HIGH ); delay(333); digitalWrite( LED_BUILTIN, LOW ); delay(333); } |
We may enhance this simple blink example with Object Oriented Programming features of C++. We can add a Tblink class with on() and off() methods and we can use it as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
void setup() { pinMode( LED_BUILTIN, OUTPUT ); } class Tblink { public: Tblink() // constroctor { digitalWrite( LED_BUILTIN, LOW ); } void on() { digitalWrite( LED_BUILTIN, HIGH ); delay(333); } void off() { digitalWrite( LED_BUILTIN, LOW ); delay(333); } }; void loop() { Tblink blink; // create a blink object with Tblink class Type, this will run its constructor method blink.on(); blink.off(); } |
as you see it is easy to use C++ features in Arduino.
How To Program Arduino With C++ Builder IDE?
In theory yes, if you send program instructions to the port of Arduino, you can. In practice though it is best to use the Arduino IDE to send the individual Arduino Sketch programs down to the Arduino device. The greatest strength is then to use C++ Builder or RAD Studio to develop C/C++ apps and projects with FMX / VCL frameworks.
Those VCL and FMX C++ apps can interact with the Arduino board signals and trigger their inputs and outputs. Here is a tutorial about how to use Delphi and serial port, C++ version would be about same, https://forum.arduino.cc/t/tutorial-arduino-delphi-10-2-and-serial-port/471049
There is also an excellent tutorial and demonstration by Embarcadero MVP Ian Barker and Technology Partner Serge Pilko showing how to use the Arduino IDE and RAD Studio together to create real IoT programs. The tutorial and video are on our sister site LearnDelphi.org: https://learndelphi.org/what-you-need-to-start-windows-development-for-iot-devices/
C++ Builder Professional, Enterprise and Architect Editions are able to run C programs. There is a free C++ Builder Community Edition for students, beginners, and startups too. You can download, install and run your C and C++ programs in console applications. Moreover you can enhance your C programs with C++ features and modern GUIs, which means you can mix and use C and C++ together. For the best modern app, later if you wish, you can update your C program to a C++ program.
Why not download a free trial of the latest version of RAD Studio now?