Thursday 3 January 2013

JY-MCU with Arduino IDE

There are some challenges to program the JY-MCU Minimum AVR System Board (ATMEGA32) using the Arduino 1.0 IDE:
  1. It is not a recognized board option, so ports are not mapped correctly.
  2. It is not one of the Arduino chips, it is an ATmega32L, so not sure everything will work properly.
  3. It comes with HID bootloader, so the IDE is unable to download the sketch with avrdude.
To my luck, Arduino IDE is able to work with custom boards: http://code.google.com/p/arduino/wiki/Platforms
This allows me to define the JY-MCU as another board and specify all the available pins and their features.

The board schematic can be found here: JY-MCU Min32 v1.2.pdf
And the files I have created to add it to the Arduino IDE can be found here: JY-MCU.zip
All you need to do is to extract the JY-MCY folder and put it into in the hardware sub-folder of your sketchbook folder (this is the Arduino folder in your Documents on a Windows operating system).

Now I can select the board in the Arduino IDE:

Here is the list of digital and analog pin allocations:

Digital | Analog | Port | Hardware & Functions
--------+--------+------+----------------------------------
 D 0    |        | PD0  | RXD  USB(D+)
 D 1    |        | PD1  | TXD
 D 2    |        | PD2  |      USB(D-)
 D 3    |        | PD3  | 
 D 4    |        | PD4  | SW1  PWM
 D 5    |        | PD5  | SW2  PWM
 D 6    |        | PD6  | SW3
 D 7    |        | PD7  | SW4  PWM
 D 8    |        | PB0  | LED8
 D 9    |        | PB1  | LED7
 D 10   |        | PB2  | LED6
 D 11   |        | PB3  | LED5
 D 12   |        | PB4  | LED4 SPI(SS)
 D 13   |        | PB5  | LED3 SPI(MOSI)
 D 14   |        | PB6  | LED2 SPI(MISO)
 D 15   |        | PB7  | LED1 SPI(SCK)
 D 16   |        | PC0  |      I2C(SCL)
 D 17   |        | PC1  |      I2C(SDA)
 D 18   |        | PC2  | 
 D 19   |        | PC3  | 
 D 20   |        | PC4  | 
 D 21   |        | PC5  | 
 D 22   |        | PC6  | 
 D 23   |        | PC7  | 
 D 24   |  A 0   | PA0  | 
 D 25   |  A 1   | PA1  | 
 D 26   |  A 2   | PA2  | 
 D 27   |  A 3   | PA3  | 
 D 28   |  A 4   | PA4  | 
 D 29   |  A 5   | PA5  | 
 D 30   |  A 6   | PA6  | 
 D 31   |  A 7   | PA7  | 

Notice that due to the differences between the ATmega328 on the Arduino boards and the ATmega32 on the JY-MCU board, not all libraries are supported. For example, the SoftwareSerial library use pin change interrupts that are not available in the ATmega32, so the library will not work.

Now I can build some code (for instance, the Blink example), but I can't program the board (upload the *.hex) directly from the IDE.

The JY-MCU board comes with an HID bootloader and this bootloader is not supported by the AVRDUDE tool that the Arduino IDE is using.

For now, I just build my code (use the Verify / Compile menu option / button) and use HDIBootFlash to upload the *.hex file to the board. After the build the *.hex file can be found in the temp files directory (on Windows: C:\Users\<user>\AppData\Local\Temp).
I hope that in future versions of Arduino it will support custom tools for uploading code to the board and I can integrate some HID bootloader tool.
Another option is to put a different bootloader on the board, one that is supported by AVRDUDE, like USBaspLoader, but for this I need a programmer hardware that I don't have at the moment.

Now to my next task: make a web server using ENC28J60 Ethernet Module.

Update (27/01/2012): I wrote a small VBS script to make it easier to upload the *.hex file to the board.
The script finds the latest *.hex file in my temp directory (this is the result of verifying the sketch in Arduino IDE), prompt to continue and then use HIDBootFlash.exe to upload it to the board.
You can get the script here: UploadHex.vbs (rename the file to UploadHex.vbs).
You need to edit the following line with the location of your HIDBootFlash.exe:
shell.ShellExecute "C:\Dev\arduino-1.0.3\HIDBootFlash.exe", " -r " & hex.Path

16 comments:

  1. Hi,

    first - thank you for blogging this stuff.
    I own this device too.
    I played a lot and tryed your adjustments.

    I can use the LED's but i can not use the buttons.
    I tried digitalRead and the bounce library.

    Have you tried the buttons?

    Thanks Frank

    ReplyDelete
    Replies
    1. Hi Frank,

      The buttons circuitry is from the pin to ground, that is, when the button is pressed, it brings the pin to ground.
      For this to work, you need to pull-up the pin to VCC. This can be done with external resistor or using the internal pull-up resistor:
      pinMode(4, INPUT_PULLUP);

      Then use (digitalRead(4) == LOW) to identify button pressed situation.

      Regards,
      Hagai

      Delete
    2. Thank you. Now everything is working :o)

      Delete
  2. Hi thanks for the very good description. I have got such a device too :). But i have a problem. Do you know how to connect a 2 line lcd display with I2C interface with the JY-MCU board. Please help i need it for a training on my school.

    Best Regards Thomas

    ReplyDelete
    Replies
    1. Hi Thomas,

      I haven't programmed with the Wire library (http://www.arduino.cc/en/Reference/Wire) and I2C devices.
      However, the I2C pins on the JY-MCY are as specified above: SCL is pin PC0 and SDA is pin PC1.
      On the breakout connector, SCL/PC0 is pin 33 and SDA/PC1 is pin 34. They are the last 2 pins, all the way to the USB/JTAG side.

      Good luck.

      Delete
  3. Hi Hagai,

    do you have the SD-Library running with this board? I've tried it but without luck.

    Thanks!

    Kind regards,

    Martin

    ReplyDelete
  4. Hi, need help, please tell me how i can receive data to usb with this id jy-mce board???

    ReplyDelete
    Replies
    1. This board does not have USB to serial hardware.
      The boot loader is implemented in software as HID device.
      You might be able to implement such USB using this: http://www.obdev.at/products/vusb/index.html

      Delete
  5. Hi! now trouble i have, please help! Why analogRead(24) didn,t work?
    I try read analog value from PA0 (24 address) but i don,t have value,
    But if i analogWrite(PA0,255) or analogWrite(24,255) power is haved in Pin

    ReplyDelete
    Replies
    1. Can you try with analogRead(0)?
      Notice that analogWrite with 255 or 0 is implemented as digitalWrite, all other values use PWM and will only work on pins with PWM (D4, D5 and D7).

      Delete
  6. Thanks, i try connect irda receiver. i create timer and check impulse count. Thanks now i know how use analog in this board.

    ReplyDelete
  7. Hi folks,
    Just got a couple of these off eBay.

    Problem #1 with a bullet is that the ISP header does not work ... need to figure out why -- even removing R5 didn't help.

    The JTAG header works, but that's no good for 95% of the people out there.

    Give this one a miss if you're looking to do Ardurino development for now.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Hi.
    Good job.
    Now i am working with JY-MEGA32_DEMO V1.3. I found some difference, buttons are on PD3-PD6.
    I am using it with USBasp programmer an programming in arduino directly.. But at the first is needed in avrdude to change fuses from 1MHz internal clock... And probably disable JTAG to get PC2-PC5 working.

    ReplyDelete