I have Googled quite a lot regarding using a C++ program for uploading to Arduino and related, however I am confused now. Right, my problem is this:

I have written a C++ program which imports RGB data of a 24 bit bitmap (which was generated using Matlab) and now I would like to use this array (3 arrays - R,G,B) for an Arduino sketch to be used for a LED matrix project that I am working on. I am not exactly a programmer and hence had to resort to this path -- MATLAB to c++ and then Arduino, although this could be entirely done in C++ too (importing a bitmap and extracting pixel info). So, my C++ code spits out the 3 arrays and I am not sure how to get those into an Arduino sketch.

I read a lot about this and I understood this - As the Arduino "language" is C and C++ therefore it is possible to program an Arduino in C++/c. I need a maker file and a compiler. However my question is that isn't there a more non-programmer friendly way to get this done, some software or an IDE which supports Arduino programming in C++? I cannot understand much of this method, hence trying to bypass this.

If anybody's got any better method, I would be really glad if you could share it. I am also looking to use the Arduino package for MATLAB to send the array directly from there using serial communication. But I just wanted to make sure I at least tried this path.

Would really appreciate if somebody could offer some help. I am quite stuck here.

Can share any other info if required.

Thanks a lot!


To clarify as Nick Gammon pointed out in the comments below. In brief steps, this is what I have done till now:

  1. Got a 24 bit bitmap (some random image)
  2. Extracted the RGB data for each pixel using Matlab
  3. Have 3 arrays of (for eg.) 122X300 dimensions whose every element has values between 0-255 (8 bit) and for R,G and B colors
  4. Want to get these 3 arrays for use in Arduino sketch for further working
  5. Stuck here - how to do that .....
  6. So I did this - I imported that the RGB data into 3 separate text files and made a c++ program to read those arrays assuming that I should be able to then somehow use this program in Arduino sketch.
  7. Now I am not sure what to do....my goal would be to have these 3 arrays (CRGB arrays, FastLED) in my arduino sketch which I can control and then upload to my Due.
  8. On googling I found that to use c++ for arduino , you need the avr compiler and make file, which I want to avoid. Tried other IDEs too, Eclipse, PlatformIO too .. ummmm ... not that successful to be honest.
  9. as per Paul's ans- Matlab to Arduino method I have to try. Hope I have clarified my process till now :)


Snapshot of Red color data (122X300) in Text format (imported from Matlab)

C++ code for reading the above array (thought I could use the output in Arduino)

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
#include <sstream>
#include <stdlib.h>

const int HEIGHT = 4;

const int WIDTH = 5;

using namespace std;

int main(){

  int array_req [HEIGHT][WIDTH];

string userinputprompt, filename;

userinputprompt = "COLORDATA FILENAME: ";



ifstream inputfile;


 for (int i = 0; i < HEIGHT; ++i)
  std::string tmpString;
  std::getline(inputfile, tmpString);

 std::stringstream ss(tmpString);

 for(int j=0;j < WIDTH; ++j)
    ss >> array_req[i][j];

    for(int p=0; p<HEIGHT; p++)
      for(int q=0; q<WIDTH; q++)
        cout<<array_req[p][q]<<" ";

return 0;

  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Nick Gammon
    Commented Apr 13, 2016 at 1:48
  • @NickGammon dont have 20 rep for chat. Would have to wait. Sorry. Commented Apr 13, 2016 at 1:51
  • 1
    You have 23 rep now. :)
    – Nick Gammon
    Commented Apr 13, 2016 at 1:57
  • Although down voted twice by people who apparently are uninformed, many of the C++ problems you are describing as it pertains to your coding environment can be overcome by using Visual Studio free edition with Visual Micro add-in fro Arduino. It makes using C++ a snap for Arduino.
    – dinotom
    Commented Apr 23, 2016 at 10:19

4 Answers 4


You've got quite an interesting project.

Programming an Arduino (C++)

Arduino is C++ (or atleast a subset).

The Arduino IDE uses the GNU AVR-GCC compiler. And it takes care of all "make" files and stuff. You shouldn't really bother too much. Just paste the code right in, and fix whatever is broken ;)

Also, the Arduino IDE is basically click&install, click&compile/upload, I'm not sure why you're even trying all the other options.

For the sake of clarity/simplicity I'll give you an example of sending a single R/G/B pixel to the Arduino.


You should set up your Matlab code something like this. I'm not a "Matlab guru" nor do I know whatever code you have. You should open a COM port and write to it.

s = serial('COM1','BAUD',115200);
Pixel pix = getPixelFromBMP("image.bmp",0);


In the Arduino IDE (withouth worrying about makefiles and toolchains).

You do the reverse of what you've done in your Matlab code. Read the serial input and write it to the LED strip. I'm quite sure this code won't compile, and there are a lot of fancier ways to read the serial buffer:

#include <ledstrip.h>

LedStrip ledStrip;

int incomingByteR = 0;   // for incoming serial data
int incomingByteG = 0;   // for incoming serial data
int incomingByteB = 0;   // for incoming serial data

void setup() {
        Serial.begin(115200);     // opens serial port, sets data rate to 115200 bps
        ledStrip.begin(1,RGB);    // initializes the ledstrip (RGB).

void loop() {
        // send data only when you receive data:
        if (Serial.available() > 3) {
                // read the incoming byte:
                incomingByteR = Serial.read();
                incomingByteG = Serial.read();
                incomingByteB = Serial.read();
                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByteR, DEC);
                Serial.println(incomingByteG, DEC);
                Serial.println(incomingByteB, DEC);
                ledStrip.pixel[0].r = incomingByteR;
                ledStrip.pixel[0].g = incomingByteG;
                ledStrip.pixel[0].b = incomingByteB;


So, you don't really need C++ (other than to program the Arduino). You could interchange Matlab for C++ or Arduino for pure AVR C++, but I wouldn't recommend it.

  • 1
    wow! your answer did clear some of my doubt. Thanks! I just realised when I was trying to use a cpp code for arduino in Codeblocks that I have to eventually go the hard way - Avr compliler,make files etc. etc...installed WinAVR and got lost again. I guess Matlab should save me. Let me try with it, your code and concept and will update here maybe tom. Hope that's fine :) Commented Apr 12, 2016 at 19:17
  • Everything is fine. You can consider accepting this as an answer. If you run in other problems, you could create a new question. But I indeed find your project interesting.
    – aaa
    Commented Apr 12, 2016 at 19:22
  • Hmmm...Right mate. I will accept it..no issues. I thought creating another similar question would be frowned upon ;) I will look into it and get back on this later. need some time. Thanks! Commented Apr 12, 2016 at 19:34
  • @fattypanda I'm not such a frowner ;) But it's very likely you'll run into a problem that's completely different. If you then update/change the question, the inital problem might get lost. If you'd like to discuss the project or minor questions related to it. I'd be happy to answer them in chat (chat.stackexchange.com/rooms/38302/…)
    – aaa
    Commented Apr 12, 2016 at 19:45
  • 1
    lol yes. .pretty nice to see though you worked with APA102. Let me see how to get around this (+2 rep min) Commented Apr 12, 2016 at 20:27

Here's a sample program you could use as reference, just replace the values in the array and adjust VALUES_NUMBER accordingly.

Disclaimer: I didn't try to compile/run it, but it should give you an idea of how to use the values you already have.

// These are Arduino Pins that support "analog" output.
const unsigned short ANALOG_RED_PIN = 3;
const unsigned short ANALOG_GREEN_PIN = 5;
const unsigned short ANALOG_BLUE_PIN = 6;

const unsigned int VALUES_NUMBER = 12;

const unsigned short REDS[VALUES_NUMBER] = {
  0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
  0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B

const unsigned short GREENS[VALUES_NUMBER] = {
  0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
  0x00, 0x01, 0x02, 0x03, 0x04, 0x05

const unsigned short BLUES[VALUES_NUMBER] = {
  0x09, 0x0A, 0x0B, 0x03, 0x04, 0x05,
  0x00, 0x01, 0x02, 0x06, 0x07, 0x08

void setup() {
  pinMode(analogRed, OUTPUT);
  pinMode(analogGreen, OUTPUT);
  pinMode(analogBlue, OUTPUT);

void loop() {
  unsigned int counter;
  for (counter = 0; counter < VALUES_NUMBER; counter++) {
    analogWrite(ANALOG_RED_PIN, REDS[counter]);
    analogWrite(ANALOG_GREEN_PIN, GREENS[counter]);
    analogWrite(ANALOG_BLUE_PIN, BLUES[counter]);
    delay(1000); //sleep 1 second
  • 1
    .thanks for the reply. Your code is a very simplified version. However, its my mistake, apologies..i didnt mention..I am implementing this project using APA102 led strips which has a data and a clock pin. So, as far as my knowledge goes, control of strip using individual analog pins is not possible. Also, the second part of the code where the data is passed on to the leds, I am done with that part. Its the first part where I am stuck. Acquiring of data in 3 different arrays. Rest is done using the fantastic FastLED lib. :) Commented Apr 12, 2016 at 19:08

As the arduino "language" is C and C++ therefore it is possible to program an Arduino in C++/c. I need a maker file and a compiler. However my question is that isn't there a more non-programmer friendly way to get this done, some software or an IDE which supports Arduino programming in C++?

Yes, it's called the Arduino IDE. You plug your board into the USB port, write your program, or use an existing example, and then hit the Upload button.

  • lol...I guess you misconstrued my question :P I meant that I wanted to be more flexible with the Arduino IDE by incorporating c++ environment. I was sort of handicapped (most probably because I am a non-programmer). Couldn't relate the output of a c++ program (or for that matter Matlab) directly into Arduino sketch. I have been using Arduino IDE for the last couple of months :) Commented Apr 12, 2016 at 23:07
  • 1
    I'm not sure where this is heading. In the Arduino IDE you are programming in C++. You don't need to install or change anything to use it - or to "incorporate the C++ environment". It's just there.
    – Nick Gammon
    Commented Apr 12, 2016 at 23:18
  • Nick Gammon is right. Recently I started programming Arduino in Eclipse though. See here if interested: arduino.stackexchange.com/questions/40/…. Also, this is very relevant: arduino.stackexchange.com/questions/816/…. Lastly, Nick Gammon is an Arduino master. His website is filled with sooo much incredibly useful and detailed information. He is one of the foremost experts in the world when it comes to the ATmega328 microcontroller and Arduino. Commented May 7, 2020 at 7:51

Use the Visual Studio free community edition (or the paid versions if you already have them) with the Visual Micro extension for Arduino. Makes life a lot easier since you have indicated your desire to write in C++, and you get true debugging and intellisense as well, which you DO NOT get with the aforementioned IDE's.

  • Could you please tell me how this is relevant to this question?
    – Avamander
    Commented Apr 13, 2016 at 18:30
  • Your first line, I have Googled quite a lot regarding using a C++ program for uploading to Arduino and related, however I am confused now. Right, my problem is this:....and that is what I answered
    – dinotom
    Commented Apr 15, 2016 at 1:18
  • There is nothing about needing debugging in the original question. If you wanted to say that one can use VS CE then edit the answer appropriately.
    – Avamander
    Commented Apr 15, 2016 at 10:13
  • @Avamander....is my answer now more suitable for you.
    – dinotom
    Commented Apr 18, 2016 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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