Details steps for using a Arduino Uno as a ISP for uploding C++ programs

Hello All,

I Just Prototyped my project (A self controlled watering system for my house) using a arduino uno board, 12 v 500 mA solenoid valve and a soil moisture sensor.

Now I want to put a dedicated board for doing the same. I would be using a ATMega328P microprocessor.

I shall be using C++ to write the progam this time to do the same job.

I am also using Ubuntu 14.0 computer.

Now I would like to know the detailed steps of how to upload this c++ program into my ATMega328p microcontroller using the arduino board ( I also do not like to remove the on board microcontroller on the arduino board).

I shall want to use the microcontroller's inbuilt 8 Mhz clock.

I tried searching the net but still confused wheather this is possible and if so How exactly to do it.

What all would I need to install in my linux computer to achieve this task.

Thanks in advance.

with regards

G.Gnanasenthil

Hi

I shall be using C++ to write the progam this time to do the same job.

You can write Arduino programs in C++ programs using the Arduino IDE, but maybe you mean you are using a different IDE to program without the Arduino libraries?

There is an excellent resource on Arduino-compatible boards by Nick Gammon here: Gammon Forum : Electronics : Microprocessors : How to make an Arduino-compatible minimal board

Regards

Ray

From a technical point of view, using ArduinoISP to load arbitrary binaries (.hex files) into a stand-alone mega328 board is almost identical to using ArduinoISP to load a bootloader into such a board. Except that there are MANY tutorials for doing the latter!

Hackscribble:
You can write Arduino programs in C++ programs using the Arduino IDE

You can do that without installing libraries?

Just by removing the setup() and loop() and replacing them with a main() containting a while(1)?

Bianco:
You can do that without installing libraries?

Just by removing the setup() and loop() and replacing them with a main() containing a while(1)?

No, fair point, you still need setup() and loop(). What I meant is that one can use many of the features of the C++ language.

Are you planning to use the features in the Arduino libraries, or do you want to treat the Atmega328P as a "raw" chip? If the latter, then maybe look at Atmel Studio - http://www.atmel.com/microsite/atmel_studio6/

Hackscribble:
No, fair point, you still need setup() and loop(). What I meant is that one can use many of the features of the C++ language.

It seems you can code in pure C (possibly C++) with a main function and everything: Programming Arduino Uno in pure C | Freedom Embedded

Obviously, it forbids you from using the Arduino functions (e.g. DigitalWrite() and so on) but might well be much faster.

Bianco:
It seems you can code in pure C (possibly C++) with a main function and everything: Programming Arduino Uno in pure C | Freedom Embedded

Obviously, it forbids you from using the Arduino functions (e.g. DigitalWrite() and so on) but might well be much faster.

That is using the AVR toolchain rather than the Arduino IDE. If you are happy to drive the tools manually, then that avoids the need to install something like Atmel Studio.

On the OP's original question, I'm still not sure if the intention was to use the Arduino libraries or not.

I shall be using C++ to write the progam this time to do the same job.

Hackscribble:
<...>
On the OP's original question, I'm still not sure if the intention was to use the Arduino libraries or not.

I could not determine what the intent was, either. If one wants to program an AVR in "pure" C++ then my recommendation would be to dump Arduino and move to Atmel's Studio. That way, the learning would apply to a more traditional business programming environment. AVRDUDE is well documented, so using an Arduini for ISP is not a real issue. A lots of the AVRDude commands are shown in my Dolly the Cloned Sheep article: Batch Duplicator

After the Op writes his own 328 C++ init routine, s/he may be running back to the Arduino.

Thank you all,

Let me tell where I got lost after searching an answer for my question.

These were the steps that I was willing to do in Order.

STEP1:

The code that I am wishing to translate to c++ is as follows

int solenoid = 13;
// assuming the power transitor connected to the solenoid is controlled through digital pin 13

void setup()
{
  pinMode(solenoid,OUTPUT);
  //digital pin 13 is set to output
  Serial.begin(9600);
}

void loop()
{
  int sensorValue = analogRead(A0);
  //the variable sensorvalue reads the analog data from pin A0
  Serial.println(sensorValue);
  
  if (sensorValue > 500)
  {
    //if the sensorValue is less than 300, the solenoid is opened for a period of 10s (temporarily, real model the value should change)
    //then closed
    digitalWrite(solenoid,HIGH);
    delay(5000);
    digitalWrite(solenoid,LOW);
  }
  delay(10000);
}

The translation I think should be pretty simple.

Lets say I compile the resulting code and get a .hex file.

STEP2:

I Upload ArduinoISP sketch to my arduino board.

STEP3:

Altering the board.txt

As explained in this page, I insert a custom board. But the page says to use an option "BOARD.upload.using=arduino:arduinoisp", but the page where these options are documented says that this option is no longer supported in arduino 1.0 , my arduino ide shows "arduino 1:1.0.5_dfsg2-2" which means that option is not supported in mine either ( I am not sure).

STEP4:
I connect my Arduino uno board and the ATMEGA328p on breadboard as shown here under "wiring" section.

STEP5:

At this point I assume I should choose the custom board that I previously inserted via board.txt under tools.
Under programmer should I choose "ARDUINO AS ISP"

STEP6:

Now if I want to burn bootloader I shall click "Burn bootloader" option under tools.

If I want to upload a arduino sketch (to my custom board) can I be using the standard upload button from the arduino IDE.

If I want to upload my .hex file how should I be proceeding (I guess avrdude should be used).

If all this went successful, I would have burned the bootloader/ my arduino sketch / .hex file but the custom board works with 16 Mhz.

If I want to lower my clock frequency ( I am thinking to use the in build 8 Mhz or lower clock frequency), what should I be doing??

To summarize:

  • What is the alternative to BOARD.upload.using in arduino 1.0 ? (My wild guess is that the "Arduino as ISP" sketch or the choosing of "Arduino ISP" shall take care of that)
  • How to set the clock frequency of the mcu of the custom board.?

I could not determine what the intent was, either

My intention of using c++ was that, c++ is more generic than arduino that's all. Of course using c++ comes the disadvantage of not able to use the diffrent arduino libraries.

This thread also has discussed things on very similar lines.

with regards
G.Gnanasenthil

What is the alternative to BOARD.upload.using in arduino 1.0 ?

At this point, you should be looking into the "alternatives in arduino 1.5" if you're intending to use the IDE for uploading.

When I write programs outside of the Arduino IDE that give me a .hex file, I just load ArduinoISP on the arduino I'm using for ISP, and then use "bare" avrdude in a command-line shell to do the upload of the .hex:

avrdude -n -p atmega2560 -P /dev/tty.usbserial-FTD61T6Q -cstk500v1 -b19200 -U flash:w:myprogram.hex -v

Or something like that.

Don't get stuck trying to use the "simple IDE" for things that it wasn't really designed to do.

At this point, you should be looking into the "alternatives in arduino 1.5" if you're intending to use the IDE for uploading.

I looked into my board.txt file," BOARD.upload.using" nowhere is this option used, nor could I find it with Google. Any Idea where to look around?

When I write programs outside of the Arduino IDE that give me a .hex file, I just load ArduinoISP on the arduino I'm using for ISP, and then use "bare" avrdude in a command-line shell to do the upload of the .hex:

Does it mean by creating a suitable entry in the board.txt (omitting the BOARD.upload.using parameter as with other entries), uploading the Arduino ISP sketch to arduino uno and choosing the Tools ---->Programmer ------> Arduino as ISP, I should be able to atleast, upload the arduino ide sketch that I posted previously to my custom board.

Hello all again,

Last Night I tried to boot load my fresh atmega328p-pu.

But unsure what happened.

This is what I did

  1. I wired the blank chip as explained by Nick Gammon in this page

  2. I uploaded the board detector sketch that was available to my uno board.

  3. I got the following output, I had a chip signature "0x1e 0x95 0x14" as opposed to "0x1e 0x95 0x0F". ( I am sure the chip is atmega328p -pu). Hence I got a unrecognized signature.

  4. So I had follow the workaround to upload the booloader that is described in this page.
    Finally the IDE showed "Done uploading" after I clicked the burnbootloader button.
    Still I am not sure If the bootloader is burned to my chip.

(Even after the above procedure the board detector sketch is showing the same output, the output does not show anything about the bootloader as it is supposed to showed instead it straight skips the "first 256 bytes of program" and shows FF FF FF throughout.
I was not able to use my uno as the ISP programmer to upload sketches to my chip, the sketch is trying to upload to the uno board even after uploading the ArduinoISP sketch to the uno board, choosing the Arduino as ISP in the programmer tab)

I thought I shall go ahead and burn a bootloader of my choice using the arduino IDE and the workaround shown above, but while trying to compile the "Atmega_board_programmer sketch" by Nick Gammon, I get the following compiling error

In file included from Atmega_Board_Programmer.ino:120:0:
bootloader_atmega168.h:3:34: error: variable ‘atmega168_optiboot’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’
 byte PROGMEM atmega168_optiboot [] = {
                                  ^
In file included from Atmega_Board_Programmer.ino:121:0:
bootloader_atmega328.h:3:34: error: variable ‘atmega328_optiboot’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’
 byte PROGMEM atmega328_optiboot [] = {
                                  ^
In file included from Atmega_Board_Programmer.ino:122:0:
bootloader_atmega2560_v2.h:50:41: error: variable ‘atmega2560_bootloader_hex’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’
 byte PROGMEM atmega2560_bootloader_hex [] = {
                                         ^
In file included from Atmega_Board_Programmer.ino:123:0:
bootloader_atmega1284.h:5:40: error: variable ‘optiboot_atmega1284p_hex’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’
 byte PROGMEM optiboot_atmega1284p_hex [] = {
                                        ^
In file included from Atmega_Board_Programmer.ino:124:0:
bootloader_lilypad328.h:5:53: error: variable ‘ATmegaBOOT_168_atmega328_pro_8MHz_hex’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’
 byte PROGMEM ATmegaBOOT_168_atmega328_pro_8MHz_hex [] = {
                                                     ^
In file included from Atmega_Board_Programmer.ino:125:0:
bootloader_atmega1280.h:5:39: error: variable ‘optiboot_atmega1280_hex’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’
 byte PROGMEM optiboot_atmega1280_hex [] = {
                                       ^
In file included from Atmega_Board_Programmer.ino:126:0:
bootloader_atmega8.h:5:27: error: variable ‘atmega8_hex’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’
 byte PROGMEM atmega8_hex [] = {
                           ^

Sort of stuck.

Any ideas.

Hello All,

I am excited to post that, finally succeeded in burning the bootloader on my ATMEGA328 chip (lilypad bootloader on my fake ATMEGA328P actually a ATMEGA328).

A successful step towards using Arduino Uno as a programmer to upload hex files created from C++ code.

I hope that others who may have such a problem in future would be benefited by this post.

First thing first, the items that I used.

  • Arduino board : Arduino Uno board
  • Microcontroller to be programmed : ATMEGA328 (to be standalone chip)
  • Operating System on computer : Ubuntu 14.04.1
  • IDE version : arduino 1:1.0.5+dfsg2-2
  • Sketch used 1 : Nick Gammon's arduino board detector sketch
  • Sketch used 2 : Nick Gammon's arduino board programmer sketch

Steps That I did:

I.Burn the bootloader

  1. I wired my standalone chip (ATMEGA328) as described in minimal wiring section of Nick Gammon's forum

  2. Connected my Arduino and the breadboard as described in the Test the chip section of the page.

  3. Uploaded the latest version of the board detector sketch of Nick Gammon's.

  4. The output was not very good though. It said that my chip had an unrecognized signature.
    ( Here again another problem cropped up, with the minimal wiring on the breadboard, I just had "Atmega chip detector" displayed on the serial monitor, nothing more. So I just tried inserting a 16 Mhz crystal in pins 9 and 10 on the breadboard along with 22 pF capacitors as shown again in the same page under "Chip not detected" section.)

  5. Now the output displayed in the serial monitor was different, but not good. It said the chip on the bread board had a unrecognised signature. It showed the signature to be "0x1e 0x95 0x14".

  6. I went ahead, Now I uploaded the latest atmega board programmer sketch (the one that I mentioned in my previous post was not recent, so It failed to compile)

  7. I wanted the Lilypad bootloader, so the necessary keyboard inputs were given appropriately through the serial monitor.
    ('L')

II.Upload a sketch on the new atmega328 with bootloader using the arduino uno as the programmer.

  1. Uploaded the ArduinoISP sketch from the arduino ide on the arduino uno board.

  2. I modified the avrdude.conf file as described here in this instructable.
    ( replaced the atmega328p signature "0x1e 0x95 0x0F" with atmega328 signature "0x1e 0x95 0x14")

  3. Chose the serial port, Programmer (Tools -> Programmer -> Arduino As ISP) and Board (Tools -> Board -> Lily Pad arduino w/ ATmega328)

  4. Opened the Led blink sketch from File -> Examples -> basic -> blink and uploaded the sketch (File -> Upload using programmer). This I think is crucial, since the upload button on the IDE uploads the sketch to the arduino uno board and not to the breadboard.

  5. Edited the avrdude.conf file to its original form.

It works.

Thank you all. That was a nice learning excercise.