Notes About Working with Various Arduino & Netduino Microcontroller Boards

Friday, May 24, 2013

Example Serial Communication between an Arduino & a Beaglebone Black (Arduino Side)


Here is an example of serial communication between an Arduino Uno and a Beaglebone Black. This example has an Adafruit NFC/RFID Shield attached to the Arduino (as part of a larger application).  The RFID shield reads an RFID tag and sends the tag's ID number to the Beaglebone Black over a serial connection using a very simple protocol. The Beaglebone Black then responds over the same connection. Eventually, I will have the Beaglebone check the tag ID against a database and record the usage of the tag, but this example does not go that far. This isn't meant to be a complete RFID or authentication example - the serial connection itself is more relevant here.

The Beaglebone Black portion of this post is here.

Connections


This example uses a Sparkfun logic level converter to bridge the voltage gap between the Arduino Uno (5V) and the Beaglebone Black (3V3).

Arduino/NFC Logic Level Conv.  Beaglebone Black
5V          HV HV
GND         HV GND
Digital 5   HV TXO / LV TXI    P9 24 (UART1_TXD)
Digital 6   HV RXI / LV RXO    P9 26 (UART1_RXD)
                            LV LV              P9 3  (3V3)
            LV GND             P9 1  (GND)


Code


You will need the Adafruit_NFCShield_I2C library to get this example to work.  This example prints output to the serial console and uses the SoftSerial library for the communication between the Arduino and the Beaglebone. 

#include <Wire.h>
#include <Adafruit_NFCShield_I2C.h>
#include <SoftwareSerial.h>

#define IRQ   (2)  // Shield's IRQ line uses D2
#define RESET (3)  // Not connected on NFC Shield
#define RX (5)     // Can use other available pins 
#define TX (6)     // for SoftSerial RX/TX

String resp = "";

Adafruit_NFCShield_I2C nfc(IRQ, RESET);

SoftwareSerial mySerial(RX, TX);

void setup(void) {
  Serial.begin(38400);
  nfc.begin();
  nfc.SAMConfig();
  
  mySerial.begin(38400);
}

void loop(void) {
  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer for returned UID
  uint8_t uidLength;         

  // Check for response from Beaglebone 
  while(mySerial.available()>0) {
    char c = mySerial.read();
    resp += c;
    if(c == '\n') {
      Serial.print(resp);
      resp = "";
    }
  }
  
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  if (success) {
     mySerial.print('^');  // Marks start of data packet
     for(int c = 0; c < uidLength; c++) {
       mySerial.print(uid[c], HEX);
     }
     mySerial.print("\r\n"); // Marks end of data packet
     mySerial.flush();
  }
  delay(500); 
}

4 comments:

  1. Shouldn't the BBB pins be on P9?

    ReplyDelete
    Replies
    1. Yes, you are correct. That should be P9. Thanks!

      Delete
  2. How would I do this through the USB port?

    ReplyDelete
    Replies
    1. Do you mean the USB port on the Beaglebone or are you using an Arduino that has an available USB port?

      Delete