For example I have an code for arduino but I want to change it to embedded C to create more professional project with different hardware. is there any tutorial? I know the logic and the most of the codes are the same. I just wanna know about libraries or pin definitions for LCD and buttons.

  • 1
    Arduino code is already C, you can even use C++ constructs (some at least). Most libraries use already C++ classes for example. You are quite vague about 'different hardware'. – Michel Keijzers Nov 25 '19 at 14:06
  • By different hardware I meant non arduino micro controller and customized LCD – Sohrab Yavarzadeh Nov 25 '19 at 14:15
  • non-arduino is sill very vague. Some MCUs don't offer a C compiler at all. Some offer a somewhat C compiler with weird syntax (PMS150C - I'm looking at you). You will have to be more specific that that. Tell us what hardware platform you are targeting and someone here will be able to give you an answer. – Kwasmich Nov 25 '19 at 15:04
  • ATMega328P this is my MC target – Sohrab Yavarzadeh Nov 26 '19 at 7:33

Arduinos use a standard C++ compiler. The supposed "Arduino Language" doesn't exist; it is C++. Except that the Arduino IDE will permit you some shortcuts that the compiler doesn't. The two biggies are:

  1. The IDE will notice that your are trying to use libraries (ones it recognizes, anyway) and insert the necessary #include <SOMELIBRARY.H>, if you didn't do it.

  2. The IDE will notice when you make forward reference to a function not yet defined nor declared, and provide the necessary forward declaration.

But if you manage these for yourself, as you would have to in any other C++ environment, your code structure will be portable.

The remaining source of incompatibility is hardware dependence. Writing embedded systems software is almost always hardware dependent. It has to be dependent on the external hardware, and quite likely is dependent on specifics of the MCU. The latter dependencies can usually be written around. Function calls or direct references to the processor's I/O registers, for example, which, on another processor, will map differently, maybe even to memory addresses instead of to registers at all.

Another processor will very likely have an entirely different set of pins with different characteristics and capabilities from the original one. Libraries to drive external hardware will likely have been designed for a given processor or processor-family and may not map gracefully to another.

If processor independence is a design-goal of the project, then you'd probably define a hypothetical set of features that you'd expect all processors to share, and hand code for anything outside of that basic set. If you wanted to take advantage of certain features if they were available on a given processors, then, in addition to the hand-coded features you wish to have, you'd also provide conditionally compiled, optimized-for-the-processor code that you could enable when you compile for that processor, and disable for all others.

| improve this answer | |
  • exactly I want to know what shortcuts Arduino gives me that other compilers don't – Sohrab Yavarzadeh Nov 26 '19 at 7:34
  • It's actually done by the IDE. The underlying compiler is a pretty plain vanilla C++ compiler. FWIW, I do what you're contemplating - write standard and complete C/C++. I don't actually port it much of anywhere; my primary purpose is to be able to use either the Arduino IDE or Eclipse/Sloeber, at will, and portable code is very effective to enable me to do that. – JRobert Nov 26 '19 at 20:53

If you would to write more professional code, you could try to use a different editor instead the basic arduino editor. Possible choices are, for example:

  • Visual Studio Core with platformio plugin.
  • Eclipse With Sloeber plugin.

Since arduino already use C/C++ Languange, the only difference is the way you write and organize the code.

| improve this answer | |
  • Amen! I am wondering if Arduino IDE 2.x will be based on something like VS Code with features that go beyond plain syntax highlighting. – Kwasmich Nov 25 '19 at 16:38
  • In my opinion Arduino IDE should be very simple. If you need more that write a simple scatch, you can use other IDE already compatible with arduino. – IU1JVO Giuliano Favro Nov 25 '19 at 16:44
  • The only thing that keeps me installing Arduino IDE is the board and library manager. As Arduino CLI doesn't seem to gain tracktion, this is unfortunately the easiest way to get startet with those. I wonder when Arduino-CLI will be stable and promoted on their page. – Kwasmich Nov 25 '19 at 16:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.