Go Down

Topic: Can I convert this to Arduino IDE (Read 3580 times) previous topic - next topic

SouthernAtHeart

I'm familiar with programming the Uno and pro mini.
I'm wondering if I could convert this little thing to run an arduino sketch, since it has a ATmega328 on it:
http://www.sparkfun.com/products/9571
If your just wanting to control a motor, with a couple sensors, it's all right there on this board!
This article talks about how to put a different firmware on it, but I would need to put the Arduino language on it, so I could make a sketch to do what I need.  This would be great if this was possible.
Any ideas?

SouthernAtHeart

Ps, I have a MacBook.
I'm. Looking at this, but I'm not sure I'm in the right place...
http://arduino.cc/playground/Learning/Burn168

kf2qd

Doen't look like it has programming pins brought out so programming it might be a problem... You can talk to it using serial commands. You could use the serial out from an Arduino board to talk to it.

SouthernAtHeart

It does have the six pin header, it's not labeled on the board, but the pins are:
MOSI, MISO, SCK, RESET, VCC, GND.  AND the TX and RX are available on the side. 
My FTDI breakout cable wouldn't plug in directly, but couldn't I wire it up somehow to load sketches on this?

BetterSense

#4
Mar 15, 2012, 10:27 pm Last Edit: Mar 15, 2012, 10:42 pm by BetterSense Reason: 1
Look at the Arduino schematic and get the pins for the 6-pin ISP header, and also for the FTDI header. If the 6-pin header has all the pins that the FTDI header does, you can wire up an adapter.

You can use a ladyada usbtinyisp to put whatever you want on it since it has the 6-pin ISP header. I believe you can load an arduino bootloader on it, or you can also just put C on it. Just using avrdude to flash C to it is like .0001 harder than using arduino; it's not a big deal.

SouthernAtHeart

In this example, the user is flashing a new firmware to the serial controlled motor driver.
Could I just use an UNO to load my sketch to it? 
http://robotics-visionexperiments.blogspot.com/2010/10/re-programming-rob-09571-sparkfun.html


I've never programmed in C, just in Arduino IDE, so not sure how to convert my sketch to C.  Does the IDE do that maybe when it compiles it?

Thanks for the help on learning about this.  If anyone knows any good tutorials that help...

BetterSense

#6
Mar 16, 2012, 03:28 pm Last Edit: Mar 16, 2012, 03:41 pm by BetterSense Reason: 1
Quote
I've never programmed in C, just in Arduino IDE, so not sure how to convert my sketch to C.  Does the IDE do that maybe when it compiles it?


First of all, programming in C is not much harder than in Arduino. The Arduino program (which I refuse to call an IDE) converts all your sketches into C before it compiles them. However, I believe that the resulting hex code will not work on a plain AVR without the Arduino bootloader installed (I would like an answer to this).

I will try to explain. This is why I hate Arduino, because by trying to make something simple, they have made it so there is a large amount of people who don't understand what is happening. By making it simpler, they make it harder.

The AVR microcontroller only understands Intel Hex code. So a compiled AVR program will be hex code, whether that hex code comes from a C compiler, or an assembler, or the Arduino program; the AVR doesn't care.  

The standard (not-arduino) way to program AVRs is to flash the hex code (which is generated either with an assembler or with your C compiler) using the 6-pin ICSP header. You normally use an ISP programmer, like a ladyada USBtinyISP, to do this, using a downloading program like avrdude. This is not hard to do. You just need an ISP device. Since the Sparkfun board has a spot for the 6-pin header, you can easily use this method. Also all Arduino boards have a 6-pin header, so you can use this method with any Arduino board and forget it is an "Arduino" at all and just use the board in the normal AVR way.

The Arduino way of programming is different. In the Arduino way, you install the Arduino bootloader program onto the AVR chip. In order to do this, you actually need to have an ISP (when you buy an Arduino board, this has already been done for you). This bootloader program does one important thing: When the AVR is first turned on, the bootloader program checks the serial port and waits to see if you are trying to download an Arduino sketch. If you are, it sucks it in the serial port and installs it. If you are not, it runs the program that is already installed. If there is no program installed, and you aren't trying to download one, the bootloader does nothing. Technically speaking, the bootloader is a waste because it just takes up program memory, and it makes the Arduino much slower to start up. An AVR programmed in the standard way can wake up and run code in less than a millisecond. With the bootloader, it takes much longer. Many hobby people don't care about this. The entire point of the bootloader is so that you can use the serial port to download programs. So if you buy a chip that already has the bootloader on it, you never need an ISP. But if your computer doesn't have a serial port (and not many do), you still need to buy a USB-to-serial or FTDI cable anyway, which isn't any cheaper than an ISP. Of course the full-blown Arduino boards have a USB-to-serial convertor built in so you can just plug them in. This is kind of silly because you are basically buying a USB-to-serial converter with every Arduino board you buy, when you really only need one and then you could reuse it for every board. But that's Arduino for you.

If you already have an Arduino board (I did not know that for sure) then you can use the arduino board as an ISP to program ANOTHER board. This is exactly the same thing as using a normal ISP programmer like a USBtinyISP; just using an arduino board as the ISP device, so you don't have to buy an ISP device. But this is still only good for flashing hex code to the target board in the standard (non-Arduino) way. That's what the guy in that website is doing. And it sounds like he didn't modify the target board firmware himself, he just downloaded a new version of the hex code and flashed it onto the target board, using his Arduino as the ISP.

When you click the "compile" button on the Arduino program, the Arduino program converts your sketch into legal C code, and then compiles it with the regular C compiler. But the hex code that you get at the end will only work with the Arduino bootloader (to my knowledge, I am not sure about this--anyone know?).

Bottom line: If you want to write custom firmware for that sparkfun board, you can do it. You can either

1. buy an ISP, or convert your UNO to be one (this sounds like a lot of trouble; ISPs are only $20, and then you won't have to reconfigure your UNO into an ISP every time you want to flash a program). You will have to write your code in regular C (this is not any harder than writing in Arduino, but you won't be able to call the Arduino libraries--no loss since most of them aren't that good anyway).  OR you may be able to flash a hex file from a compiled Arduino program directly to the AVR (I don't think this works, but someone may correct me. It would be great if it did).

2. flash the Arduino bootloader onto it the chip so you can download programs from the Arduino.  But in order to download Arduino sketches, even with the Arduino bootloader on the chip, you will need need the FTDI pin connections, which I don't know if the sparkfun board brings those out. You will need an ISP to flash the bootloader anyway, so if you have to buy an ISP, or configure your UNO into one, you might as just use it for programming the sparkfun board directly (the 6-pin header is right there for you to use) and forget about trying to use Arduino with it.


SouthernAtHeart

Thanks!!!  This was VERY helpful.  I understand what's happening a lot better now!  Appriciate the time you took to explain it.  I understand it will work, but I'm still not quite sure of the steps I need, though.  Let me tell you the options I'll choose, and if you can fill in the missing steps...
1)  I would like to write the code in the Arduino IDE environment, as I'm familiar with that (I self taught). 
2)  I would like to convert my UNO into an ISP.  So I load the ArduinoISP sketch onto it, correct?
3)  Then to use my UNO as the ISP (In System Programmer--just reading up on that a bit), so I would connect the serial motor board to my UNO with the ICSP headers as in the webpage:
http://robotics-visionexperiments.blogspot.com/2010/10/re-programming-rob-09571-sparkfun.html
4) Now, to get my compiled sketch on the second board, do I have to use what he used on that webpage (WINAVR) or is there a way to use the Arduino IDE to send the two files (main.hex & eep.hex) to the second board.  I know that normally the Arduino IDE loads the sketch directly onto the UNO, via Serial, but does it have an option to load this second board, now that my UNO is the 'ISP'?
thanks!


http://
So what steps would I take? 
Can I write the sketch compile it in Arduino IDE, and it is created/stored somewhere in the form of the files needed (main.hex, and main.eep)?
Can I use the Arduino IDE somehow to load it through my UNO via it's ICSP header connected to this motor board's ICSP?
...or do I need that other program --WINAVR...


JChristensen

#8
Mar 17, 2012, 04:41 am Last Edit: Mar 17, 2012, 04:45 am by Jack Christensen Reason: 1

First of all, programming in C is not much harder than in Arduino. The Arduino program (which I refuse to call an IDE) converts all your sketches into C before it compiles them. However, I believe that the resulting hex code will not work on a plain AVR without the Arduino bootloader installed (I would like an answer to this).


Arduino source code is C. Well, C++ actually. The hex code generated will indeed work on a plain AVR without a bootloader. In fact, the Arduino (sort of, as you say :)) IDE can program directly to a plain AVR via an ICSP programmer. The bootloader is simply another route to get the hex code into the AVR. Once a sketch is running, there is no interaction with the bootloader. The hex code has to be compatible, after all, the hardware only understands one thing, namely AVR machine language.

BetterSense

#9
Mar 17, 2012, 04:45 am Last Edit: Mar 17, 2012, 04:48 am by BetterSense Reason: 1
Quote
1)  I would like to write the code in the Arduino IDE environment, as I'm familiar with that (I self taught).  


Ok. But if you do that, I don't know if the .hex files that you will get from the Arduino IDE will work on an AVR without the Arduino bootloader (I have never tried it).

Quote
2)  I would like to convert my UNO into an ISP.  So I load the ArduinoISP sketch onto it, correct?

Yes. If you load the ArduinoISP sketch onto your UNO and wire it up like the instructions, you can use it as an ISP.

Quote
4) Now, to get my compiled sketch on the second board, do I have to use what he used on that webpage (WINAVR) or is there a way to use the Arduino IDE to send the two files (main.hex & eep.hex) to the second board.

I don't know if you can use the Arduino program to flash .hex code with an ISP. I have never tried, but you can use avrdude which is free in all senses. It sounds like you can also use WinAVR (I have never used that program and am not familiar with it).

It would be really nice if you can just flash Arduino binarys directly to an AVR and have them work. The above post suggests that you can, which is good news.

SouthernAtHeart



Arduino source code is C. Well, C++ actually. The hex code generated will indeed work on a plain AVR without a bootloader. In fact, the Arduino (sort of, as you say :)) IDE can program directly to a plain AVR via an ICSP programmer.

GREAT!  SO I can write my sketch and use my UNO as the ISP and load the hex directly on the board.  
How???
I see in the Arduino IDE  :smiley-zipper: there is Tools/Programmer/Arduino as IDE.  Surely it's not as simple as selecting that, and it dumps my Arduino sketch, converted to hex code straight thru the UNO, unto the motor board???  That would be great!

JChristensen


GREAT!  SO I can write my sketch and use my UNO as the ISP and load the hex directly on the board.  
How???


In the IDE, see File > Examples > ArduinoISP. There's tons of forum threads on it, also check the main Arduino web site. I haven't ever actually used it, I just got a dedicated ICSP programmer instead. For $20 or so, money very well spent IMHO. Better to have a dedicated device.

Quote

I see in the Arduino IDE  :smiley-zipper: there is Tools/Programmer/Arduino as IDE.  Surely it's not as simple as selecting that, and it dumps my Arduino sketch, converted to hex code straight thru the UNO, unto the motor board???  That would be great!


Correct. The Arduino has to be running the ArduinoISP sketch. No bootloader required on the target chip.

SouthernAtHeart


In the IDE, see File > Examples > ArduinoISP. There's tons of forum threads on it, also check the main Arduino web site. I haven't ever actually used it, I just got a dedicated ICSP programmer instead. For $20 or so, money very well spent IMHO. Better to have a dedicated device.

Quote

I see in the Arduino IDE  :smiley-zipper: there is Tools/Programmer/Arduino as IDE.  Surely it's not as simple as selecting that, and it dumps my Arduino sketch, converted to hex code straight thru the UNO, unto the motor board???  That would be great!


Correct. The Arduino has to be running the ArduinoISP sketch. No bootloader required on the target chip.


THANK YOU, AGAIN.  I think I've about got it in my head now!
I'm just a tinkerer, so using the UNO as the ICSP is fine for me.

So just to be certain I've got it, the simple steps are:
1) load the ISP Sketch on the UNO.
2) connect the UNO to the Sparkfun board via the ICSP headers (properly, from that webpage)
3) write my new firmware sketch for the Sparkfun board.
4) change the setting to Tools/Programmer/Arduino as ISP
...and compile/load!
That's it?

JChristensen


THANK YOU, AGAIN.  I think I've about got it in my head now!
I'm just a tinkerer, so using the UNO as the ICSP is fine for me.


Yes it should be fine especially if just occasional. I really should try it myself some day :smiley-red:

Quote

So just to be certain I've got it, the simple steps are:
1) load the ISP Sketch on the UNO.
2) connect the UNO to the Sparkfun board via the ICSP headers (properly, from that webpage)
3) write my new firmware sketch for the Sparkfun board.
4) change the setting to Tools/Programmer/Arduino as ISP
...and compile/load!
That's it?


Sounds right. Understand that you will be overwriting the firmware that comes on that Sparkfun board, so your sketch will need to provide some replacement functionality if you intend to control the motors.

I'm not familiar with that Sparkfun board, have never touched one, but I did download the firmware just to have a quick look. It is not meant to be compiled with the Arduino IDE, probably WinAVR would work. They did not include the hex file in the zip I downloaded. So you may be burning a bit of a bridge the first time you reprogram the Sparkfun board. Their source code would need to be recompiled to create a hex file. Just so you're aware. The other thing that could be done, is before programming the board, read the flash with AVRDUDE into a hex file on your computer. Then it can be re-uploaded to the board if you want.

SouthernAtHeart

Thanks, yes that all makes sense.  I see what you mean about burning bridges, but it shouldn't be a problem.

Go Up