Digital I/O Pins on evive

Description
Learn how to configure and use Digital I/O pins on evive with Arduino IDE and PictoBlox (Scratch). We will also explore digitalRead() and digitalWrite() functions, and how to use the read status of the digital pin () block and Set digital pin () output as () block in PictoBlox.

Introduction

Under the Magic Lid of evive, there are 28 Digital 5V Input and Output (I/O) pins.

Digital I/O stands for Digital Input and Output. Digital Inputs allow a microcontroller to detect logic states, and Digital Outputs allow a microcontroller to output logic states.

Digital Input: A digital input detects if a voltage is above/below a specific threshold. If the voltage is higher than some value, evive will detect the digital input as HIGH/1. If the voltage is lower than some value, evive will detect the digital input as LOW/0.

Digital Output: A digital output allows you to control a voltage with evive. If evive instructs the output to be high, the output will produce a voltage (generally about 5 or 3.3 volts). If evive instructs the output to be LOW, it is connected to the ground and produces no voltage.

Controlling digital pins in Arduino IDE

Using the digital pin as Input

evive digital I/O pins are default assigned as inputs and hence they don’t need to be explicitly declared as input initially in the program. But it is advisable that one should declare each and every pin as input or output for consistency.

Digital Input pins can be configured as pinMode(pin, INPUT), where the pin is the digital pin number you want to initialize. Often it is useful to steer an input pin to a known state if no input is present. This can be done by adding a pull-up resistor (to +5V), or a pulldown resistor (resistor to ground) on the input. A 10K resistor is a good value for a pull-up or pulldown resistor.

If the pin is configured as INPUT_PULLUP during initialization, it inverts the behavior of the INPUT mode, where HIGH means the sensor is OFF and LOW means the sensor is ON. It is because there are 20K pull-up resistors built into the Atmega chip that can be accessed from software.

Using the digital pin as Output

Pins can be configured as OUTPUT with pinMode(pin, OUTPUT), where the pin is the digital pin number you want to initialize as output. These pins are also in a low-impedance state. This means that they can provide a substantial amount of current to other circuits. Atmega pins can source (provide positive current) or sink (provide negative current) up to 40 mA (milliamps) of current to other devices/circuits. This is enough current to brightly light up an LED (don’t forget the series resistor), or run many sensors, for example, but not enough current to run most relays, solenoids, or motors.

Short circuits on digital pins, or attempting to run high current devices from them, can damage or destroy the output transistors in the pin, or damage the entire Atmega chip. Often this will result in a “dead” pin in the microcontroller but the remaining chip will still function adequately. For this reason, it is a good idea to connect OUTPUT pins to other devices with 470Ω or 1kΩ resistors, unless maximum current draw from the pins is required for a particular application.

digitalWrite()

Using digitalWrite() function in Arduino IDE, you can write a digital pin, to a HIGH or LOW value.

If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V for HIGH, 0V for LOW.

If the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal pull-up on the input pin. It is recommended to set the pinMode() to INPUT_PULLUP to enable the internal pull-up resistor.

The analog input pins can also be used as digital pins, referred to as A0, A1, etc.

Example

/*
 This program demonstrate the use of digitalWrite() functions. It 
 sets the pin 13 to HIGH, makes one-second long delay, and sets 
 the pin back to LOW and then again makes one-second long delay.
 */

int LED = 13;                                // LED connected to digital pin 13

void setup() {
  pinMode(LED, OUTPUT);                      // configure the digital pin as OUTPUT
}

void loop() {
  digitalWrite(LED, HIGH);                   // sets the LED on (5V on pin 13)
  delay(1000);
  digitalWrite(LED, LOW);                    // sets the LED off (0V on pin 13)
  delay(1000);
}

digitalRead()

Using digitalRead() function in Arduino IDE, you can read the value from a specific digital pin as HIGH or LOW.

Example


/*
 This program demonstrate the use of digitalRead() function. It sets 
 the digital
 */
int LEDPin = 13;                              // LED connected to digital pin 13
int BUTTONPin = 38;                           // pushbutton connected to digital pin 38
int val = 0;                                  // variable to store the read value

void setup() {
  pinMode(LEDPin, OUTPUT);                    // sets the digital pin 13 as output
  pinMode(BUTTONPin, INPUT);                  // sets the digital pin 7 as input
}

void loop() {
  val = digitalRead(BUTTONPin);               // read the input pin
  digitalWrite(LEDPin, val);                  // sets the LED to the button's value
}

Controlling digital pins in PictoBlox (Scratch)

Digital Output

Set digital pin () output as () block is used for digital output on a digital pin. It sets the digital state of the specified digital pin as either “High” or “Low”.

  1. If the output is High, the pin will be at 5V and
  2. If the output is Low the pin will be at 0V.

set digital pin output as

The pin can be selected from the drop-down menu. This block can be used in Upload Mode as well as in Stage Mode.

Example

Controlling Pin 13 LED using the Space key. If the output is High, the LED glows, and if the output is Low, the LED turns off.

digital write example

Digital Read

The read status of the digital pin () block available in evive extension is used for digital read in PictoBlox. It reads the state of the digital pin on evive (“High” or “Low”). If the state of the pin is “High”, it returns True, else False.

read state of digital pin

Available  pins for digital read-in evive:

  • 26 digital pins available for the user (2-27)

You can choose these pins from the drop-down menu.

This block can be used in Upload Mode as well as in Stage Mode.

Example

Displaying the state of the tactile switch in Scratch Mode.

read digital state example

Conclusion

In this lesson, we learned about Digital I/O pins on evive, and how to configure and use them in Arduino IDE and PictoBlox (Scratch). Digital I/O pins are used to detect and control logic states using digital input/output functions. We also learned about the digitalRead() and digitalWrite() functions, and how they can be used to read and write to digital pins. Finally, we saw how to use the read status of the digital pin () block and Set digital pin () output as () block in PictoBlox to read and write digital pins. We hope you have a better understanding of Digital I/O pins now!

Table of Contents