More Examples for the Esplora

Using the input sensors and output actuators built onto the Esplora board is a little different than using the general inputs and outputs on other Arduino boards. As a result, the example sketches included with the Arduino IDE need a little modification before you can use them with the Esplora. This guide explains how to do it.

1 | Add the Esplora Library

You need to add the Esplora library to any sketch you want to use on the Esplora. To do so, choose Import Library... -> Esplora from the Sketch menu and the IDE will automatically add the following at the top of your sketch:

#include <Esplora.h>

2 | Digital Input and Output Changes

The other Arduino boards have two types of inputs: digital inputs, which have only two states, HIGH or LOW, and analog inputs, which have a varying range of states, typically from 0 to 1023. The pushbuttons on the Esplora are digital inputs, and the other sensors are analog inputs. To adapt the regular examples to the Esplora, you can substitute the pushbutton inputs for the digital inputs, and any of the other sensors for the analog inputs.

The digital inputs on the other Arduino boards can also be used as outputs, so they need to be declared as either input or output using a command called pinMode(). Because all of the Esplora's inputs or outputs are dedicated to one function, you don't need pinMode commands.

Whenever you see the command digitalRead() in an Arduino example, substitute the command Esplora.readButton(). Choose whichever button you want to read. Below, you can see the original DigitalReadSerial example (found in File -> Examples -> 01.Basics -> DigitalReadSerial) and the modified version that works on the Esplora using Switch 1:

Original example:

  1. // digital pin 2 has a pushbutton attached to it. Give it a name:
  2. int pushButton = 2;                        // you don't need this line for the Esplora
  3.  
  4. // the setup routine runs once when you press reset:
  5. void setup() {
  6.   // initialize serial communication at 9600 bits per second:
  7.   Serial.begin(9600);
  8.   // make the pushbutton's pin an input:
  9.   pinMode(pushButton, INPUT);       // you don't need this line for the Esplora
  10. }
  11.  
  12. // the loop routine runs over and over again forever:
  13. void loop() {
  14.   // read the input pin:
  15.   int buttonState = digitalRead(pushButton); // this line needs to change
  16.   // print out the state of the button:
  17.   Serial.println(buttonState);
  18.   delay(1);        // delay in between reads for stability
  19. }

Modified example for use with Esplora:

  1. #include <Esplora.h>     // you need to include the Esplora Library
  2. // the setup routine runs once when you press reset:
  3. void setup() {
  4.   // initialize serial communication at 9600 bits per second:
  5.   Serial.begin(9600);
  6. }
  7.  
  8. // the loop routine runs over and over again forever:
  9. void loop() {
  10.   // read the input pin:
  11.   int buttonState = Esplora.readButton(SWITCH_2);  // Esplora.readButton replaces digitalRead()
  12.   // print out the state of the button:
  13.   Serial.println(buttonState);
  14.   delay(1);        // delay in between reads for stability
  15. }

Most of the Arduino digitalRead() examples are written with the assumption that the pushbutton or switch attached to them will go HIGH when pressed, and LOW when not pressed. The Esplora's pushbuttons go LOW when pressed and HIGH when not pressed. So if you want to read when the Esplora's buttons are pressed, read for LOW instead of HIGH. If you want to read when they're not pressed, read for HIGH instead of LOW.

For most Arduino examples, LEDs are used as digital outputs, meaning that you can only turn them on or off (HIGH or LOW). The RGB LED on the Esplora, however, is used as an analog output, meaning that you can set its brightness from 0 to 255. To make it act like a digital output, set its level to 255 for HIGH and 0 for LOW.

Below, you'll see the Button example (found in File -> 02.Digital -> Button) in its original and modified forms:

Original example: Highlit lines will change. Pin constants and pinMode() commands will go away because they're not needed.

  1. // constants won't change. They're used here to
  2. // set pin numbers:
  3. const int buttonPin = 2;     // the number of the pushbutton pin
  4. const int ledPin =  13;      // the number of the LED pin
  5.  
  6. // variables will change:
  7. int buttonState = 0;         // variable for reading the pushbutton status
  8.  
  9. void setup() {
  10.   // initialize the LED pin as an output:
  11.   pinMode(ledPin, OUTPUT);      
  12.   // initialize the pushbutton pin as an input:
  13.   pinMode(buttonPin, INPUT);    
  14. }
  15.  
  16. void loop(){
  17.   // read the state of the pushbutton value:
  18.   buttonState = digitalRead(buttonPin);
  19.  
  20.   // check if the pushbutton is pressed.
  21.   // if it is, the buttonState is HIGH:
  22.   if (buttonState == HIGH) {    
  23.     // turn LED on:    
  24.     digitalWrite(ledPin, HIGH);  
  25.   }
  26.   else {
  27.     // turn LED off:
  28.     digitalWrite(ledPin, LOW);
  29.   }
  30. }

Modified example for use with Esplora:

  1. #include <Esplora.h>   // you need to include the Esplora library
  2.  
  3. int buttonState = 0;   // variable for reading the pushbutton status
  4.  
  5. void setup() {
  6.  // nothing to set up
  7. }
  8.  
  9. void loop(){
  10.   // read the state of the pushbutton value:
  11.   // Esplora.readButton replaces digitalRead()
  12.   buttonState = Esplora.readButton(SWITCH_1);
  13.  
  14.   // check if the pushbutton is pressed.
  15.   // if it is, the buttonState is LOW:
  16.   if (buttonState == LOW) {     // Button is pushed when LOW, not HIGH
  17.     // turn LED on:    
  18.     Esplora.writeRed(255);  // Esplora.writeRed() replaces digitalWrite()
  19.   }
  20.   else {
  21.     // turn LED off:
  22.     Esplora.writeRed(0);  // Esplora.writeRed() replaces digitalWrite()
  23.   }
  24. }

You don't have to use only the red LED channel; you can use any of the RGB channels as digital outputs in this way. Likewise, you can use any of the pushbuttons to replace digital inputs.

3 | Analog Input and Output Changes

Changes to analog examples are similar to the digital ones. There's no pinMode() commands to remove, however, because the analog inputs on the other Arduino boards are inputs by default.

Here's another example, this time replacing an analog input with one the Esplora's analog inputs. The ReadAnalogVoltage example (found in File -> 01.Basics -> ReadAnalogVoltage) reads an analog input and tells you the voltage on the pin. Below, you'll replace the analogRead() command with the reading from the light sensor:

Original example:

  1. // the setup routine runs once when you press reset:
  2. void setup() {
  3.   // initialize serial communication at 9600 bits per second:
  4.   Serial.begin(9600);
  5. }
  6.  
  7. // the loop routine runs over and over again forever:
  8. void loop() {
  9.   // read the input on analog pin 0:
  10.   int sensorValue = analogRead(A0);    // You need to change this line
  11.   // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  12.   float voltage = sensorValue * (5.0 / 1023.0);
  13.   // print out the value you read:
  14.   Serial.println(voltage);
  15. }

Modified example for use with Esplora:

  1. #include <Esplora.h>     // you need to include the Esplora Library
  2. // the setup routine runs once when you press reset:
  3. void setup() {
  4.   // initialize serial communication at 9600 bits per second:
  5.   Serial.begin(9600);
  6. }
  7.  
  8. // the loop routine runs over and over again forever:
  9. void loop() {
  10.   // read the input on analog pin 0:
  11.   int sensorValue = Esplora.readLightSensor();  //Esplora.readLightSensor() replaces analogRead()
  12.   // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  13.   float voltage = sensorValue * (5.0 / 1023.0);
  14.   // print out the value you read:
  15.   Serial.println(voltage);
  16. }

Most of the analog sensors on the Esplora can be substituted for analogRead() statements as-is, because they are simply that: analog sensors. The joystick is made of two potentiometers, so you can think of each of its axes as a single analogRead(). Similarly, the accelerometer can be thought of three analogRead() channels, one for each axis.

The temperature sensor on the Esplora is different from the other analog sensors, however. The Esplora.readTemperature() command doesn't simply give you the analog reading like the other sensor commands do. Instead, it converts the sensor's reading to Celsius or Fahrenheit. So you can't simply substitute the temperature sensor for an analogRead() command.

The analogWrite() command on other Arduinos works only on certain pins. Its range is from 0 to 255, just like the Esplora.writeRed(), Esplora.writeGreen(), and Esplora.writeBlue() commands, so you could replace analogWrite() statements with any of these three commands and see a working result. Note that analogWrite() takes two parameters, a pin number and a brightness level, while the Esplora.writeRed() and related commands only take one parameter, the brightness. Below you can see the Fade example (found in File -> Examples -> 01.Basics -> Fade) in its original and modified forms:

Original example: pinMode() command will be cut, as it's not needed.

  1. int led = 9;           // the pin that the LED is attached to
  2. int brightness = 0;    // how bright the LED is
  3. int fadeAmount = 5;    // how many points to fade the LED by
  4.  
  5. // the setup routine runs once when you press reset:
  6. void setup()  {
  7.   // declare pin 9 to be an output:
  8.   pinMode(led, OUTPUT);
  9. }
  10.  
  11. // the loop routine runs over and over again forever:
  12. void loop()  {
  13.   // set the brightness of pin 9:
  14.   analogWrite(led, brightness);    
  15.  
  16.   // change the brightness for next time through the loop:
  17.   brightness = brightness + fadeAmount;
  18.  
  19.   // reverse the direction of the fading at the ends of the fade:
  20.   if (brightness == 0 || brightness == 255) {
  21.     fadeAmount = -fadeAmount ;
  22.   }    
  23.   // wait for 30 milliseconds to see the dimming effect    
  24.   delay(30);                            
  25. }
  26.  

Modified example for use with Esplora:

  1. #include <Esplora.h>   // you need to include the Esplora library
  2.  
  3. int brightness = 0;    // how bright the LED is
  4. int fadeAmount = 5;    // how many points to fade the LED by
  5.  
  6. // the setup routine runs once when you press reset:
  7. void setup()  {
  8.  // nothing to set up
  9. }
  10.  
  11. // the loop routine runs over and over again forever:
  12. void loop()  {
  13.   // set the brightness of the blue channel:
  14.   Esplora.writeBlue(brightness);    // Esplora.writeBlue() replaces analogWrite()
  15.  
  16.   // change the brightness for next time through the loop:
  17.   brightness = brightness + fadeAmount;
  18.  
  19.   // reverse the direction of the fading at the ends of the fade:
  20.   if (brightness == 0 || brightness == 255) {
  21.     fadeAmount = -fadeAmount ;
  22.   }    
  23.   // wait for 30 milliseconds to see the dimming effect    
  24.   delay(30);                            
  25. }

4 | Communicating With Your Computer via USB

Serial communication over USB using the Serial.read(), Serial.write(), Serial.print and Serial.println() commands should work on the Esplora just the same as they do on other boards. However, the serial output of the Esplora works slightly faster than the Uno, so you might want to add a short delay to sketches that do nothing but read a sensor and serially print the result, so as not to fill your computer's serial input buffer. When your computer's serial buffer fills up, the Serial Monitor will run much slower, and you'll experience a delay when you change windows from it to the main IDE. A delay of even 1 millisecond will work fine.

You can also use the USB Mouse and Keyboard libraries on the Esplora. The examples found in File -> Examples -> 09.USB will work with only modifications for digital and analog I/O, as described above. There's an example for the Esplora called EsploraJoystickMouse that lets you use the joystick as a mouse controller as well.

5 | Communicating With Other Devices

The other Arduinos offer two other forms of serial communication, SPI (using the SPI library) and I2C (using the Wire library). The Esplora can communicate via SPI using the ICSP header that's also used for optional in-circuit serial programming of the board. The ICSP connector's pins are laid out as follows. Pin 1 is the pin nearest the white dot on the Esplora board. It's the bottom right pin if you're holding the Esplora with the USB connector facing up:

To connect an SPI device to the Esplora, you'll have to make your own connector cable for this connector.

The Esplora doesn't expose pins to provide for I2C communication, so you won't be able to use examples that use the Wire library with the Esplora.

Generally, if you need SPI or I2C connectivity, you're better off using another model of Arduino.

6 | After The Esplora

Once you've mastered the Esplora, if you're looking for other Arduino boards to try, the next best step is the Arduino Uno, which is the heart of the Arduino line. It allows you to connect your own sensor and actuator circuits, or add-on shields for expanded capability. You might also want to consider the Arduino Leonardo. It's based on the same processor as the Esplora, and can likewise act as a USB keyboard or mouse. It offers all of the functionality of the regular Arduino boards as well. The text of the Arduino getting started guide is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the guide are released into the public domain.