Sunday, March 24, 2013

Reading the Arduino Esplora Tinkerkit Inputs

The Esplora is expandable via two Tinketkit Outputs and two Tinkerkit Inputs.  At present the Arduino IDE does not have code to easily use these connectors.  This post will provide information on how you can read the inputs before the Arduino team releases their code.
The two white Esplora Tinkerkit Inputs IN-A and IN-B to the right of the USB cable
The two Tinkerkit Inputs are the white three-pin connectors to the right of the USB port.  They cannot be read directly by analogRead as they are connected through the Esplora's 74HC4067D multiplexer chip.  The multiplexer allows more sensors to connect to the ATMEGA 32U4 microcontroller.  The multiplexer address lines are connected to the ATMEGA logical analog lines A0, A1, A2, and A3.  The result is read on A4 when the address lines point to the desired input.

/*
  Esplora Tinkerkit Input Read

 This sketch shows you how to read the Tinkerkit Inputs on the Arduino Esplora.
 Most likely this program will become obsolete when the Arduino team updates the IDE beyond 1.04

 Created on 2013-03-24 by Mike Barela

 This example is in the public domain, please attribute
 */

#include <Esplora.h>
#if ARDUINO < 105
const byte CH_TINKERKIT_INA = 8;   // Add values missing from Esplora.h
const byte CH_TINKERKIT_INB = 9;
const byte INPUT_A          = 0;
const byte INPUT_B          = 1;

unsigned int readTinkerkitInput(byte whichInput) {      // return 0-1023 from Tinkerkit Input A or B
   return readChannel(whichInput+CH_TINKERKIT_INA); }   //   as defined above
 
unsigned int readChannel(byte channel) {                // as Esplora.readChannel is a private function
     digitalWrite(A0, (channel & 1) ? HIGH : LOW);      //  we declare our own as a hack
     digitalWrite(A1, (channel & 2) ? HIGH : LOW);      //
     digitalWrite(A2, (channel & 4) ? HIGH : LOW);      // digitalWrite sets address lines for the input
     digitalWrite(A3, (channel & 8) ? HIGH : LOW);
     return analogRead(A4);               // analogRead gets value from MUX chip
}
#endif

void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
}

void loop() {
  // read the sensor into a variable:
  unsigned int input_a_value = readTinkerkitInput(INPUT_A);
  unsigned int input_b_value = readTinkerkitInput(INPUT_B);

  // print the input values to serial monitor

  Serial.print("Input A: ");
  Serial.print(input_a_value);
  Serial.print(", Input B: ");
  Serial.println(input_b_value);
  // add a delay between readings (not required in general)
  delay(1000);
}



The Tinkerkit Inputs are on multiplexer addresses 8 and 9.  These are not defined in the esplora.h file as of IDE version 1.04.  Also the function used to read an inputs from the multiplexer, readChannel.  So these are recreated in the example above.  Every second, IN-A and IN-B are read then printed to the serial monitor.  As you see in the picture above, the IN-A data pin (center) is connected to +5 volts left pin).   The IN-B data pin is connected to the ground pin on the left).  This produces 1023 for IN-A and 0 for IN-B.  Note: if you have nothing connected to an input, it will return a random value between 0 and 1023 like any analog input.

The two Tinkerkit outputs are to the left of the USB connector and are orange.  If you wish to read or write to them it is more straightforward.  OUT-B is on Arduino logical pin D11 and OUT-A is on pin D3.  These are digital pins but they are pulse width modulation enabled so analogWrite works also.  Previous programs on this site have used these connectors to send data, mainly to an xbee.  See this one for more.

If you are interested in other Esplora articles on this website, a list is here.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.