banner

Saturday, April 21, 2012

Running PYTHON (pymite-09) on an Arduino MEGA 2560

Hi, I got a new toy from farnell. It is an Arduino mega 2560 board
in.element14.com/atmel


 

Now it is the first time I am using an arduino board. Arduino mega 2560 is really a great product. The chip got a flash of 256KB, RAM of 8KB and EEPROM of 4KB. Also, the data sheet of Atmega2560 says that we can extend the RAM (by external) upto 64KB. Another most important feature of this stuff is that it have 54 Digital I/O pins (of which 14 provide PWM output). It is clocked with a 16MHz crystal osc. Also the board contains another atmega16 (just above the crystal) which is pre-programmed as a USB to UART converter which enables a serial communitation between the atmega2560 and  PC via USB. The product is shipped with a bootloader inside which make it easy to program it via the same USB-UART channel. So the channel got two functions, ie programming the chip and serial communication with PC. From the arduino home page we can download the arduino IDE for appropriate OS.
      I heard arduino makes people lazy. ;-) But now I understood it is true. Because it is so easy to program an arduino using the arduino IDE. It have an arduino language (it is c++) with a great library functions those are very much user friendly and I heard people saying, "if it is arduino, any one can program it". Now I believe statement is almost right because in arduino IDE, we doesn't need to know any thing about the processor, it's registers and even its real port address also... Every thing is grouped and numbered in the board not according to the real PORTA, PORTB etc but according to it's functioning like PWM, ANALOG, DIGITAL, COMMUNICATION etc.
The arduino IDE got a lot of working examples, what we need to do is, we need to select the board first and then set the serial port and later we can open any example and then simply press 'upload'. It is ready!!!!!!!! ;-)
So no need to explain it more because it is so simple..

Running Python (pymite) on Arduino mega 2560
Appart from the arduino IDE, I would like to use the board seperately to try assembly programming and the normal C programming which I normally used to do using avr-gcc. 1 year ago I heard some one ported python for 8 bit devices but I couldn't use it since I didn't have a chip with enough RAM and flash. But since now I have this arduino mega 2560, I just downloaded pymite (p14p) and I could see a port for arduino mega there. It is nothing but the same "python" programming language written for microcontrollers. It have many limitations but still its interesting to see the python running on this 8 bit chip. I just build it for arduino mega 2560 and was successful (a small trouble was there in building, but any way it's okay).


Step by step (in linux terminal):
Download the pymite source from python-on-a-chip
(already explained in the README)
  • extract it. (tar -xvf pymite-xx.tar.gz)
  • 'cd' into the directory of pymite folder
  • now, cd into src/platform/arduino_mega and open the Makefile
  • edit the platform configuration MCU to atmega2560
  • now cd back to the pymite folder (cd ../../../)
  • now type "make PLATFORM=arduino_mega"
  • now if you see any error as below: 

avr-gcc -c -mmcu=atmega2560 -I. -gstabs -DF_CPU=16000000UL -I../../vm -I/home/vinod/pymite-09/src/platform/arduino_mega -Os -Wall -Wstrict-prototypes -Werror -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums  main_nat.c -o main_nat.o 
In file included from main_nat.c:20:0:
/usr/lib/gcc/avr/4.5.3/../../../avr/include/util/delay.h: In function 'nat_05_avr_delay':
/usr/lib/gcc/avr/4.5.3/../../../avr/include/util/delay.h:153:28: error: __builtin_avr_delay_cycles expects an integer constant.
/usr/lib/gcc/avr/4.5.3/../../../avr/include/util/delay.h:153:28: error: __builtin_avr_delay_cycles expects an integer constant.
make[1]: *** [main_nat.o] Error 1
make[1]: Leaving directory `/home/vinod/pymite-09/src/platform/arduino_mega'
make: *** [all] Error 2
  • if the above error occur, then open the file src/platform/arduino_mega/
  • Now add below function under the #include <util/delay.h>
//-------------------
void delay_MS(unsigned int n){
unsigned int x;
  while(n--){
    x=2600;
    while(x--);
  }
}
  • Now edit the function nat_05_avr_delay(pPmFrame_t *ppframe) in the opened file. Replace the _delay_ms( with delay_MS( and save it.
  • Now go back to pymite (cd ../../../) and type "make PLATFORM=arduino_mega" or type simply "make" from the current folder and if no errors, we could see a main.elf file generated in the arduino_mega folder.
  • Now convert it to hex using below command.
  • avr-objcopy -j .text -j .data -O ihex main.elf python.hex
  • Now we obtained the hex file and we need to burn it to arduino mega 2560, for that we can use avrdude command as below.
  • sudo avrdude -V -F -c stk500v2 -p m2560 -b 115200 -P /dev/ttyACM0 -U flash:w:python.hex
  • Now the hex is loaded into arduino and we can test it. For that, cd into the directory of the arduino_mega (cd src/platform/arduino_mega) and type   ../../tools/ipm.py -f pmfeatures.py --serial /dev/ttyACM0 --baud=57600
  • Now if every thing is okay, then we could see an ipm prompt and we can test simple python codes. Below is some of my test codes and screen shots.


     
    Arduino ethernet shield: 
     I got one more product from element14, an arduino ethernet shield. I tried the basic examples of google search, twitter etc on it and it is working fine. Also there is a micro sd card slot which is very much helpful to easily interface a micro sd card to the arduino.. It is a cool stuff....
    LINKS:
in.element14.com/atmel
     

2 comments:

Janosch said...

Hi, very nice article. I'd love to see python running on my Arduino. I followed your instructions and everything works fine until I try to execute a python statement. Here is what I get: http://pastebin.com/C5KcuQY1
Would be nice if you could take a look, since I'm rather helpless.

Liza Jenifer said...

You have shared superb article, I think statement is almost right because in arduino IDE, we doesn't need to know any thing about the processor, it's registers and even its real port address also... Every thing is grouped and numbered in the board not according to the real PORTA, PORTB etc. I would like to say Thanks for sharing this article here.
Touch Screen Watch

Post a Comment

SHARE IT on FB