Introduction to Arduino Microcontroller Boards for Behavior Analysts

Adopting technology developed in other disciplines (i.e., exogenous technology, Lattal, 2008) has been common practice in the experimental analysis of behavior. A detailed history was narrated previously (Escobar, 2014). In that review, the author also described that a new stage of adoption of technology is taking place as easy-to-use and inexpensive technology has become available.

Microcontrollers are miniature computers in a single integrated circuit. They execute one set of instructions at a time and are most appropriate for embedded applications.

One example of such technology are microcontroller boards. Microcontrollers are miniature computers in a single integrated circuit. They execute one set of instructions at a time and are most appropriate for embedded applications. These applications generally involve monitoring one aspect of the environment (receiving inputs) and executing actions (generating outputs). To name only a few, microcontrollers can be found in printers, power tools, alarm systems, and toys. Microcontroller boards are designed, among other functions, to facilitate the interaction of the microcontroller with numerous input devices such as buttons, switches, light and touch sensors, and to output devices such as LEDs, buzzers, and different types of motors. Behavior analysts could use these boards for automating projects with high precision, for example, for generating schedules of reinforcement.

Behavior analysts could use these boards for automating projects with high precision, for example, for generating schedules of reinforcement.

Microcontrollers have been available since the 1970s but they were difficult to use (e.g., assembly language programming was required). In recent years, some companies have developed platforms that include pre-assembled microcontroller boards and easy-to-use software for programming the microcontroller using simplified versions of C language. Some examples of these platforms are Propeller®, PicAXE, and Arduino, but the latter is the most popular.

Arduino platform attracted a large community of users because it is easy to use and Arduino boards can be found easily. This community has posted diagrams and programming code for controlling almost any imaginable electronic device with an Arduino board. At least basic programming skills are required for using Arduino boards but Arduino programming language is easy to learn, and as was noted by Potter, Roy, and Bianchi, 2014, these skills could be a “powerful tool allowing behavior analysts to discover functional relationships and basic principles as well as spreading behavioral technology” (p. 182). This review describes briefly how Arduino platform works and exemplifies how it can be used by behavior analysts with two simulations of simple schedules of reinforcement.

At least basic programming skills are required for using Arduino boards but Arduino programming language is easy to learn, and as was noted by Potter, Roy, and Bianchi, 2014, these skills could be a “powerful tool allowing behavior analysts to discover functional relationships and basic principles as well as spreading behavioral technology” (p. 182).

Arduino Boards

Arduino platform is divided into Arduino boards and Arduino Integrated Development Environment (IDE) software. Arduino boards come in different sizes that fit different projects and offer different specifications. The most popular is Arduino Uno board. This $25 dollar board offers 14 digital input/output pins and 6 analog input pins, which can also be used as additional digital input/output pins. A digital pin can take, or read, one of two states: on or off (also high or low, 1 or 0). Analog pins will not be used in the following examples but can read different values. Arduino Uno boards are powered with a USB port or a power supply. They operate with 5 V DC but can receive a maximum of 20 V DC from an AC to DC adapter or a battery. The USB port is also used for uploading programs from a host computer to the microcontroller. They have 32 KB of flash memory (non volatile), which is used to store programs, and 2 KB of SRAM memory (volatile), which is used to execute programs. A 16 MHz crystal oscillator is used for timing events. D’Ausilio (2012), and Schubert, D’Ausilio, and Canto (2013) tested the Arduino Uno boards in behavioral research and reported that the boards provided 1-millisecond resolution with accuracy and precision.

Other popular Arduino board is the Mega 2560. Mega boards provide 54 digital input/output pins and 16 analog input pins, which can also be used as digital input/output pins. Mega boards have 128 KB of flash memory and 8 KB of SRAM memory. These boards are $46 dollars and larger than Uno boards but can be used for more complex projects. Arduino Nano and Micro boards are smaller than Uno boards and can be used in projects in which size is an issue. As can be noted, a drawback of Arduino boards is that memory size limits the length of projects. They are not adequate for projects that require high memory capacity like video or audio analyzers. For those projects, a Raspberry Pi or a BeagleBone board could be a better choice. Even though Arduino boards excel at connecting to external output and input devices, only devices that draw less than 50 mA of current can be connected directly to the board. For connecting an LED, for example, a resistor must be added to reduce the draw of current.

Inputs and Outputs

In this post, I will describe how an Arduino Uno board can be used for generating two simple simulations of fixed-ratio and fixed-interval schedules of reinforcement. Before analyzing the code, it is necessary to connect two components. One is a pushbutton that will be used for entering responses and the other is an LED (5 or 10 mm) that will be used to simulate reinforcement delivery. The pushbutton could be substituted with any other input device (e.g., a lever switch), and the LED could be substituted with a small motor. If, however, a larger motor (> 50 mA) such as those used in feeders in operant chambers is required, it is necessary to add an external power supply with a protection circuit (see e.g., Escobar & Pérez-Herrera, in press).

The following diagram (created with Fritzing software) shows the connections using an Arduino Uno board. Switches in pushbuttons have two connectors. These connectors are physically separated from each other (circuit open). When the button is pressed, the connectors touch each other allowing current to flow from one contact to the other (circuit closed). In the example, one end of the button goes to ground and the other to Pin 9. The negative end of the LED (short lead) is connected to ground, and the positive end (long lead) is connected to Pin 9. As shown in the diagram, a 330 ohm resistor in series with the LED, is required. Jumper wires or 22 AWG solid-core wire could be used to connect the electronic devices to the Arduino board.

Microcontroller Figure 1

Arduino IDE

Arduino IDE software runs on Windows, Mac OS, and Linux. Detailed instructions for software and hardware installation are available in Arduino website (http://arduino.cc/en/Guide/HomePage). The IDE allows writing and uploading programs to the microcontroller with one click (see the following diagram). After the code is uploaded to the microcontroller, it is executed immediately. The serial monitor (see the following diagram) allows displaying and sending data to the Arduino board. The diagram shows a screen capture of the Arduino IDE software with some important buttons and sections identified.

Microcontroller Figure 2

Arduino Programs

In Arduino programs, variables are declared in the first lines. Following the example, two variables are needed; one will represent Pin 8 and the other Pin 9. Labels are used to ease programming. Comments are added after “//” or between “/* */”.

byte Button = 9; // Variable “Button” has a value of 8 that correspond to the input pin

byte LED = 8;   // Variable “LED” has a value of 9 that correspond to the output pin

These variables are declared as byte because the value is an unsigned number smaller than 255. They could also be declared as integers (int) with a range from -32,768 to 32,767 but they would use a larger amount of SRAM memory. For a fixed-ratio schedule, two additional variables will be used, one for counting responses and another for avoiding counting responses when the button is continuously pressed.

int ResponseCount = 0; // This declares the variable as integer

boolean ButtonPrevious = 1; // This variable can only take a value of 0 or 1

After variable declaration, Arduino programs are divided in two sections, setup(), in which pins are initialized as inputs or outputs at startup, and Loop(), in which the program is executed consecutively. Pin 8 (LED) is initialized as output and Pin 9 (Button) as input. For inputs connected as shown in the previous diagram, an internal pull-up resistor must be activated to avoid random fluctuations of the input value when the circuit is opened. The instruction “Serial.begin” initiates serial communication. The Arduino board sends data to a serial communication port (USB) where the Arduino IDE (or other programs) reads it and displays it on the screen when the serial monitor is opened.

void setup(){

Serial.begin(9600); // Initiates serial communication

pinMode(Button, INPUT); // set pin to input

digitalWrite(Button, HIGH);  // turn on pull-up resistor (0 = response, 1 = no response)

pinMode(LED, OUTPUT);    // set pin to output

}

In the loop() section, a fixed-ratio 10 is programmed with:

void loop(){

if (digitalRead(Button) == 0 && ButtonPrevious == 1){ // If a response is detected…

ResponseCount ++;   // Add a response to the counter

Serial.println(ResponseCount); // Sends the value of the counter to the serial port

ButtonPrevious = 0;    // Sets the previous state of the input to avoid repetitions

if (ResponseCount == 10){ // If the counter reaches the criterion…

digitalWrite (LED, HIGH);   // Turn on the LED

delay(100);        // Wait 100 ms

digitalWrite(LED, LOW);   // Turn off the LED

ResponseCount = 0;    // Reset the counter

}

}

delay(100); // Necessary for debouncing the input

}

A fixed-interval schedule requires adding timers. The function “millis()” returns the number of milliseconds since the program started. Two new variables declared as “unsigned long” are required. These variables can take a value from 0 to 4,294,967,295.

unsigned long IntervalStart = 0; /* This variable corresponds to the beginning of the interval*/

IntervalTimer = 0; // This variable will be used for timing the interval

To determine at which point the interval starts, the instruction “IntervalStart = millis()” is added within the setup() function and after each reinforcer delivery. The loop() function is modified in the following way:

void loop(){

if (digitalRead(Button) == 0 && ButtonPrevious == 1){ //If a response is detected…

ResponseCount ++; // Add a response to a counter

Serial.println(ResponseCount);// Show the number of responses

ButtonPrevious = 0; // The button is pressed

if (IntervalTimer > 10000){ // If more than 10000 ms elapsed…

digitalWrite (LED, HIGH);   // Turn on the LED

delay(100);        // Wait 100 ms

digitalWrite(LED, LOW);   // Turn off the LED

IntervalStart = millis(); // Reset the timer after reinforcement delivery

}

}

if (digitalRead(Button) == 1) { // If no response is detected…

ButtonPrevious = 1; // The button is not pressed

}

IntervalTimer = (millis() – IntervalStart); // This is the FI timer (current time – start time)  

delay(100);

}

As can be noted in these examples, Arduino programming code is relatively simple and intuitive. This post describes only two simple examples of schedules of reinforcement generated using an Arduino microcontroller with a few basic instructions. Using programs similar to those exemplified, Arduino boards could be used for laboratory demonstrations. This use of microcontroller boards was reviewed previously (Escobar, 2014). For operant research requiring more complex procedures, Arduino boards can be used in combination with Visual Basic programming. The interested readers can consult the paper by Escobar and Pérez-Herrera (in press) for detailed instructions for using such interface.

References

D’Ausilio, A. (2012). Arduino: A low-cost multipurpose lab equipment. Behavior Research Methods, 44, 305-313.

Escobar, R. (2014). From relays to microcontrollers: The adoption of technology in operant research. Mexican Journal of Behavior Analysis, 40(2), 127-153.

Escobar, R., & Pérez-Herrera, C. A. (in press). Low-cost USB interface for operant research using Arduino and Visual Basic. Journal of the Experimental Analysis of Behavior. 

Lattal, K. A. (2008). JEAB at 50: Coevolution of research and technology. Journal of the Experimental Analysis of Behavior, 89, 129-135.

Potter, B., Roy, R., & Bianchi, S. (2014). Computer programming for research and application: LiveCode development environment. Mexican Journal of Behavior Analysis, 40(2), 154-191.

Schubert T., D’Ausilio A., & Canto R. (2013). Using Arduino microcontroller boards to measure response latencies. Behavior Research Methods, 45, 1332-1346.


Rogelio Escobar is an Associate Professor of Psychology at the National Autonomous University of Mexico. He received the 2012 International Development Grant from the Society for the Advancement of Behavior Analysis for applying new technologies to the design of inexpensive control equipment for operant and applied research. Rogelio also enjoys studying and documenting the history of instruments in behavior analysis and general psychology.  


 

One comment

  1. Hello blogger, i found this post on 19 spot in google’s search results.
    I’m sure that your low rankings are caused by high bounce
    rate. This is very important ranking factor. One of the biggest
    reason for high bounce rate is due to visitors hitting the back button. The higher your bounce rate the further down the search results your posts and pages will end
    up, so having reasonably low bounce rate is important
    for increasing your rankings naturally. There is very handy wp plugin which can help you.
    Just search in google for:
    Seyiny’s Bounce Plugin

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: