Kegbot / kegboard

Kegbot's Arduino Kegerator controller, a flexible multi-function board. Firmware, schematics, Python library. Flow sensing, DS1820 temperature sensing, relay control, ID-12 RFID, and buzzer support.
http://kegbot.org/kegboard
GNU General Public License v2.0
58 stars 43 forks source link

RFID reader doesn't work with Arduino Mega 2560. #14

Closed scottresnik closed 10 years ago

scottresnik commented 10 years ago

When a standard kegboard with design date of 8/8/2011 is connected to a Arduino Mega 2560, the RFID reader is non-functional. When connected to the serial monitor, the led will flash when a kegbot store RFID tag is brought up close, but no data is seen on the monitor. The buzzer doesn't acknowledge either.

scottresnik commented 10 years ago

https://spreadsheets.google.com/a/liquidintellect.com/spreadsheet/pub?key=0AtfNMvfWhA_ccnRId19SNmVWTDE0MEtTOV9HOEdQa0E&gid=0

Looks like it's pin out differences.

mik3y commented 10 years ago

It looks like this is a subtle incompatibility with Mega: the logical pins (D0, D1, ...) still map to the same physical location, but they in some cases map to pins on the mcu with different capabilities.

ID12 RFID requires Software Serial support, which only works with PCINT capable pins. Unfortunately the pin we use (D6) does not support PCINT on the mega.

Here's something you can try: These pins support PCINT on Mega:

10, 11, 12, 13,  50, 51, 52, 53,  62, 63, 64, 65, 66, 67, 68, 69

Pick one and change KB_PIN_SERIAL_RFID_RX in kegboard_config.h accordingly. (We already use 10-13 so pick one of the others.)

Recompile and reflash. Using a jumper wire, connect pin D6 on the Kegboard shield to the pin you selected. Then give it a try.

scottresnik commented 10 years ago

I'll give that a try a report back.

scottresnik commented 10 years ago

So jumping pin 6 to pin 53 (only picked it for it's physical accessiblity), and RFID works on my mega now.

n8gr8 commented 6 years ago

Sorry to ping an old thread (I'm a bit late on the KegBot bandwagon), but maybe someone here can help resolve a similar issue? Days of toiling have gone nowhere.

Issue: Wiegand RFID does not work on Arduino Mega.

Setup: I'm using an Arduino Mega 2560 with a Wiegand RFID reader setup per the wiki post. Connecting the reader to pin A4/A5 works fine with Arduino UNO, but fails for Mega.

If I enable Wiegand #define KB_ENABLE_WIEGAND_RFID 1 (and disable ID-12), then the default PIN settings `#define KB_PIN_WIEGAND_RFID_DATA0 A4

define KB_PIN_WIEGAND_RFID_DATA1 A5` cause everything to fail (flow meters and RFID do not work) kegboard_config reference.

If I reassign the PINs for RFID to anything else (e.g. 20-21, 51,53), then flow meters work but RFID still fails. It seems that pins 2,3,18,19,20,21 are preferred by others for Wiegand RFID example.

Based on the chart (below), the Mega pinout is quite different than the Uno and I'm at a loss to figure out why things are not compatible, or whether they even can be compatible.

Is there some pin assignment where Wiegand RFID will work on the Mega? I know that 6 taps are enabled by default for the Mega (pins 2,3,18,19,20,21) -- if two are disabled can those pins be dedicated to RFID? When I attempt this (I tried this, and all the pins listed above by @mik3y), it does not work.

Any help is appreciated. Thanks for making Kegbot -- it's seriously awesome.

Official Arduinos

| PCINT |  Uno/Nano/Mini  |   Mega/2560    | Leonardo/Micro | HL2 (8/16/32u2) |
| ----- | --------------- | -------------- | -------------- | --------------- |
|     0 |  8       (PB0)  | 53 SS   (PB0)  |    SS   (PB0)* |  0 SS   (PB0)*  |
|     1 |  9       (PB1)  | 52 SCK  (PB1)  |    SCK  (PB1)  |  1 SCK  (PB1)   |
|     2 | 10 SS    (PB2)  | 51 MOSI (PB2)  |    MOSI (PB2)  |  2 MOSI (PB2)   |
|     3 | 11 MISO  (PB3)  | 50 MISO (PB3)  |    MISO (PB3)  |  3 MISO (PB3)   |
|     4 | 12 MOSI  (PB4)  | 10      (PB4)  |  8/A8   (PB4)  |  4      (PB4)   |
|     5 | 13 SCK   (PB5)  | 11      (PB5)  |  9/A9   (PB5)  |  5      (PB5)   |
|     6 |    XTAL1 (PB6)* | 12      (PB6)  | 10/A10  (PB6)  |  6      (PB6)   |
|     7 |    XTAL2 (PB7)* | 13      (PB7)  | 11      (PB7)  |  7      (PB7)   |
| ----- | --------------- | -------------- | -------------- | --------------- |
|     8 | A0       (PC0)  |  0 RX   (PE0)* |                |         (PC6)*  |
|     9 | A1       (PC1)  | 15 RX3  (PJ0)* |                |         (PC5)*  |
|    10 | A2       (PC2)  | 14 TX3  (PJ1)* |                |         (PC4)*  |
|    11 | A3       (PC3)  |    NC   (PJ2)* |                |         (PC2)*  |
|    12 | A4 SDA   (PC4)  |    NC   (PJ3)* |                |         (PD5)*  |
|    13 | A5 SDC   (PC5)  |    NC   (PJ4)* |                |                 |
|    14 |    RST   (PC6)* |    NC   (PJ5)* |                |                 |
|    15 |                 |    NC   (PJ6)* |                |                 |
| ----- | --------------- | -------------- | -------------- | --------------- |
|    16 |  0 RX    (PD0)  | A8      (PK0)  |                |                 |
|    17 |  1 TX    (PD1)  | A9      (PK1)  |                |                 |
|    18 |  2 INT0  (PD2)  | A10     (PK2)  |                |                 |
|    19 |  3 INT1  (PD3)  | A11     (PK3)  |                |                 |
|    20 |  4       (PD4)  | A12     (PK4)  |                |                 |
|    21 |  5       (PD5)  | A13     (PK5)  |                |                 |
|    22 |  6       (PD6)  | A14     (PK6)  |                |                 |
|    23 |  7       (PD7)  | A15     (PK7)  |                |                 |
| ----- | --------------- | -------------- | -------------- | --------------- |
szechyjs commented 6 years ago

Its been awhile since I played with wiegand and the interrupts. But if I recall correctly, the megas have 6 hardware interrupts (2,3,18,19,20,21), and the others only have 2 (2,3), the default code uses these for the flow meters. The wiegand interface is using the PcInt ability to get additional interrupts on any other pins. One important thing to note here is that the attachInterrupt(n, fnc, mode) function handles the mapping of n to a pin, this value will always be 0-5 on a mega, which translates to pins 2,3,18,19,20,21. When using PCattachInterrupt(pin, fnc, mode), pin is a reference to an actual pin. From what I can tell the PCInterrupt functionality was written for the Atmega168, so I wouldn't be surprised if it has bugs related to the mega. Based on your chart above you might want to try using pins A8-A15

If you don't plan on using all the flow meters on the mega you can certainly repurpose their regular hardware interrupts for wiegand. In that case simply make the following changes.

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define KB_NUM_METERS             6
#define KB_PIN_METER_C            21
#define KB_PIN_METER_D            20
#define KB_PIN_METER_E            19
#define KB_PIN_METER_F            18
#else
PCattachInterrupt(KB_PIN_WIEGAND_RFID_DATA0, wiegandData0, FALLING);
PCattachInterrupt(KB_PIN_WIEGAND_RFID_DATA1, wiegandData1, FALLING);

to

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define KB_NUM_METERS             4
#define KB_PIN_METER_C            21
#define KB_PIN_METER_D            20
#else
attachInterrupt(4, wiegandData0, FALLING);
attachInterrupt(5, wiegandData1, FALLING);
n8gr8 commented 6 years ago

@szechyjs You have no idea how satisfying it was to compile the code and see it work. Thanks so much for your help!! Based on your recommended changes, the code compiles fine and the Mega uses pins 2,3,20,21 as taps, and pins 18,19 for RFID.

I made a few changes to the Arduino code to automatically make these adjustments IFF using a Mega. Not sure whether this is worthy of a pull request or whether there is any interest in updating the codebase. (I'm also not an embedded programmer, so perhaps there is a more elegant way to do it.) Let me know if I should submit a PR or just document things here for those that come later. These changes are verified with my own setup and behave as expected for UNO and MEGA boards.

Kegboard_Config.h changes original code

// Using ATMega board?
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 
#define KB_BOARD_ATMEGA 1
#endif
...
...
// Atmega1280 (aka Arduino mega) section
#if KB_BOARD_ATMEGA  
  // Wiegand requires 2 hardware interrupt pins, meaning NUM_METERS = 4 and 
  // pins 18/19 are used for RFID, else NUM_METERS = 6
  #ifdef KB_ENABLE_WIEGAND_RFID
  #define KB_NUM_METERS             4
  #define KB_PIN_METER_C            21
  #define KB_PIN_METER_D            20
  #else
  #define KB_NUM_METERS             6
  #define KB_PIN_METER_C            21
  #define KB_PIN_METER_D            20
  #define KB_PIN_METER_E            19
  #define KB_PIN_METER_F            18
  #endif // KB_ENABLE_WIEGAND_RFID == 1
#else
#define KB_NUM_METERS             2
#endif // KB_BOARD_ATMEGA

Kegboard_Config.h change original code

#if KB_ENABLE_WIEGAND_RFID
  #if KB_BOARD_ATMEGA   // Hardcoded interrupt pin assigment for ATMega board 4=18, 5=19)
  attachInterrupt(4, wiegandData0, FALLING);
  attachInterrupt(5, wiegandData1, FALLING);
  #else 
  PCattachInterrupt(KB_PIN_WIEGAND_RFID_DATA0, wiegandData0, FALLING);
  PCattachInterrupt(KB_PIN_WIEGAND_RFID_DATA1, wiegandData1, FALLING);
  #endif // KB_BOARD_ATMEGA
  pinMode(KB_PIN_WIEGAND_RFID_DATA0, OUTPUT);
  pinMode(KB_PIN_WIEGAND_RFID_DATA1, OUTPUT);
  digitalWrite(KB_PIN_WIEGAND_RFID_DATA0, HIGH);
  digitalWrite(KB_PIN_WIEGAND_RFID_DATA1, HIGH);
  digitalWrite(KB_PIN_WIEGAND_RFID_DATA0, LOW);
  digitalWrite(KB_PIN_WIEGAND_RFID_DATA1, LOW);
  pinMode(KB_PIN_WIEGAND_RFID_DATA0, INPUT);
  pinMode(KB_PIN_WIEGAND_RFID_DATA1, INPUT);
  digitalWrite(KB_PIN_WIEGAND_RFID_DATA0, HIGH);
  digitalWrite(KB_PIN_WIEGAND_RFID_DATA1, HIGH);
  gWiegand.reset();
  gLastWiegandInterruptMillis = 0;
#endif

With the changes above, the MEGA sacrifices two taps when running Wiegand RFID. Updated pinout:

MEGA Pin Description
2 Flowmeter 0
3 Flowmeter 1
21 Flowmeter 2
20 Flowmeter 3
18 RFID 0
19 RFID 1

Thanks again! I really appreciate your help.

szechyjs commented 6 years ago

@n8gr8 Glad I was able to help. The real solution to merge back in would be to figure out what pins work with PcInt on the mega, as the hardware interrupts are not required for it to work.

n8gr8 commented 6 years ago

Thanks, @szechyjs! If I ever manage to find a pin-mapping solution I'll update. Everything in my setup (3 taps + RFID) works great now -- I really appreciate the help

mik3y commented 6 years ago

The real solution to merge back in would be to figure out what pins work with PcInt on the mega, as the hardware interrupts are not required for it to work.

👍 - I don't have a mega handy, but happy to accept at PR!