Go Down

Topic: Noob's Guide To Setting Up Codeblocks for Arduino Uno on Windows 7 (Read 16121 times) previous topic - next topic


Hello everybody. I am new to Arduino and new to c++. I wanted to move from the Arduino development environment for a variety of reasons. Based on what I read, and, in particular from this post http://arduino.cc/forum/index.php/topic,37631.0.html I spend about 4 days figuring out how to do it. I succeeded.

I think the problem is that some of the information is dated, or not directly relevant to my platform. I gave up trying to figure out why, but I ran into all kinds of problems which led me to new problems. Thanks to Google, and the fact I am stuck in a wheelchair for a few weeks, I was able to get, step by step, to the point where it all works. I am sure this is easy for a lot of people, but I'm an old guy and having slung code for a long time. I carefully (I hope) documented the process. I present it here for posterity. If anybody tries this and finds a problem, please let me know.

Here goes

General Warnings
I'm going to pretend your Windows 7 user name is mingy_desktop, but you;ll have to change it as needed.

Make sure you do not have a user name with an embedded space eg: mingy_desktop is, mingy desktop is not.
Unhide the C:\Users\mingy_desktop\AppData directory. I did this through the command prompt: Go to c:\users\mingy_desktop and type appdata attrib -H

If at some point you are trying again or 'stuck' uninstall CodeBlocks and delete all relevant associated data, which is found in C:\Users\mingy_desktop\AppData\Roaming\codeblocks. Remove the directory

Understand that every time you modify ArduinoProject.cbp, you have to close the project and create a new one. CodeBlocks will not recognize those project changes on an existing project.

1 )   Start Arduino and compile a simple sketch, like blink. Without closing Arduino, open Explorer and search for 'core.a'. Make a folder C:\arduino-022\libArduinoCoreAT328p and copy core.a to that folder. Arduino core.a when it closes, so it can bet tricky to find. Mine was in C:\Users\mingy_desktop\Local\Temp\build7176365035724894050.tmp

2 )   Install WinAVR

3 )   Install Codeblocks

4 )   Run Codeblocks
         a.   Set GNU AVR GCC compiler as your default compiler
         b.   File New Project - AVR Project
         c.   Make sure C:\WINAVR-20100100\avr\include is in your search directories (Complier and debugger setting) (or wherever your winavr include directory is)
         d.   May as well make sure C:\arduino-022\hardware\arduinocores\arduino is in your search directories (You'll need to add a bunch more of these as you develop more and more code)
         e.   File Save Project as Template and name it ArduinoProject

5 )   Close Codeblocks

6 )   Find the Codeblocks User Templates. Mine were in C:\Users\mingy_desktop\AppData\Roaming\codeblocks\UserTemplates\ArduinoProject

7 )   Copy the attached file start.cpp to (eg) C:\Users\mingy_desktop\AppData\Roaming\codeblocks\UserTemplates\ArduinoProject\start.cpp

8 )   Copy the attached ArduinoProject.cbp C:\Users\mingy_desktop\AppData\Roaming\codeblocks\UserTemplates\ArduinoProject\ArduinoProject.cbp

9 )   Note that the ArduinoProject.cbp file may have to be modified in that your COM port may not be COM3 (check in Arduino or the device manager and avrdude.exe may be elsewhere. I believe the one which comes with Arduino is out of date, thats why I use the version which comes with WinAVR

10)   Now, run CodeBlocks and File New FromTemplate User Templates Arduino Project. Tell it to create the project in a folder with no spaces in the name like AVRTest and with a name with no spaces like ArduinoProject.

11)   Select Build Target Debug and build the project. Most errors are because the Settings Compiler and Debugger Search Directories need to be filled in. Note that if you save source files like start.cpp from CodeBlocks, they'll be saved to the project directory, not the templates directory, so use notepad or something to edit them in the templates directory.

12)   Make sure Arduino is closed, plug in your Arduino Uno, Select Build Target Release and build the project. You should compile fine because you have already tested that, however you may have problems with the programmer. If that happens, copy the command line which gets echoed in the CodeBlocks Log & Others window at the bottom of the screen. Open a DOS shell and past that line into the command line.

      C:\WinAVR-20100110\bin\avrdude.exe -CC:\WinAVR-20100110\bin\avrdude.conf -V -F -c arduino -pm328p -P COM3 -U flash:w:D:\TempAVR\bin\Release\ArduinoProject.elf.hex:a

It is easier to figure out what is wrong from the DOS command line because you can edit the line over and over. You can you a working command line as a prototype for the ArduinProject.cpb line.

13)   If everything works ok you should get a message like
                 avrdude.exe: 13294 bytes of flash written
                 avrdude.exe done.  Thank you.
                 Process terminated with status 0 (0 minutes, 3 seconds)
                 0 errors, 0 warnings

        If you get some sort of synch or protocol error, check to make sure your com port settings agree with the ArduinoProject.cbp settings you made. Baud rate doesn't seem to matter

14)   Open the Arduino serial port monitor. You should see a message
                 Hello from Arduino Code::Blocks template

Thats it!

I hope this proves to be of some use to someone!

Good Luck


noobs guide on setting up code::blocks for arduino uno on linux (kubuntu)

I assume linux OS, arduino uno atmega 328P, and quite some mileage in code::blocks

1 - create project folder:  /home/me/AvrProject_2018

2 - install code::blocks

3 - install arduino IDE

4 - install avr-gcc and avrdude toolchain: ~$apt-get install avrdude binutils-avr gcc-avr avr-libc gdb-avr

5 - open arduino IDE, compile a basic blink sketch selecting the correct arduino avr chip, find the temp .o file (called core.o), rename this file to lib_atmega328P-core.o, copy this file to /home/me/AvrProject_2018/arduino

6 - find the arduino header files, usually situated in arduino-installation-directory/hardware/arduino/avr/cores/arduino/ and copy all .h files into /home/me/AvrProject_2018/arduino, don't foget the pins_arduino.h in arduino-installation-directory/hardware/arduino/avr/variants/standard/

7 - Run Codeblocks

8 - top menu -> settings -> global compiler settings -> set to "GNU AVR GCC compiler", DON"T FORGET to also, in the "search directories" subtab of this menu ->  
  • Compiler: add "/usr/lib/avr/include"
  • linker: add "usr/lib/avr/lib"
9 - codeblocks -> new project -> AVR -> select folder /home/me/AvrProject_2018 and create project ArduinoUnoProject -> Debug and Release is OK -> atmega 328p and only select F_CPU define and create hex files -> done

10 - right click ArduinoUnoProject and go to build options
  • in master tab (on left) -> linker settings -> link library -> add -> find -> /home/me/AvrProject_2018/arduinolib_atmega328P-core.o -> OK
  • in debug tab -> pre/post build steps- > post build steps- > add
    avr-objcopy -R .eeprom -R .fuse -R .lock -R .signature -O ihex $(TARGET_OUTPUT_FILE) $(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).hex
    avr-objcopy --no-change-warnings -j .eeprom --change-section-lma .eeprom=0 -O ihex $(TARGET_OUTPUT_FILE) $(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).eep

  • release  tab -> pre/post build steps- > post build steps- > add
    avr-objcopy -R .eeprom -R .fuse -R .lock -R .signature -O ihex $(TARGET_OUTPUT_FILE) $(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).hex
    avr-objcopy --no-change-warnings -j .eeprom --change-section-lma .eeprom=0 -O ihex $(TARGET_OUTPUT_FILE) $(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).eep
    avrdude -v -patmega328p -carduino -P/dev/ttyACM0 -b115200 -D -Uflash:w:$(TARGET_OUTPUT_FILE) $(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).hex
11 - right click ArduinoUnoProject and add files recursively form folder /home/me/AvrProject_2018/arduino

12 - open main.c file in your project and C-V below code

Code: [Select]
#include "arduino/Arduino.h"

int main(){

    init(); //Don't forget this!









13 - save -> debug build -> sort out errors and warnings and typo's and c-v errors and stuff -> repeat 13 till 'process terminated with status 0'

14 - save -> plug arduino uno into USB and check if correct port is used in your avrdudu release post build steps! -> relase build -> sort out errors and warnings and typo's and c-v errors and stuff -> repeat 13 till 'process terminated with status 0'

15 - your uno should blink fraction of a second, like it is in standby :)

interesting info here:
arduino avr Eclipse setup
arduino avr codeblocks programmer
avrfreaks codeblocks setup avrdude

Go Up