Digital Inputs and Outputs
Why Use Digital IO ( input/output ):
Digital inputs and outputs are plentiful on the ATMEL chip that makes up the heart of the SAMrI and are very easy to add and control!
Outputs give you the ability to turn on and off external devices such as LED's, buzzers, relays, Infra-red transmitters, and other low current devices
Digital inputs allow you to read the on/off conditions of input devices like switches, digital sensors, IR sensors etc.
The microcontroller also has several analog inputs on Port C which allow your device to read in variable voltages from zero to 5 Volts to help you measure outputs from light sensors, temperature sensors, and other variable signal devices
These IO pins are grouped into three "Ports" Port B, C, and D. Each port is a group of IO pins
In the image below, the pins of the ATMEGA328 chip are described. The Red Text refers to those pins on the SAMrINO which are dedicated to specific uses on the SAMrINO robot.
How to wire up a Digital Input /Output:
The SAMrI has most of its IO pins available to you through female header strips connected to those port pins ( shown as orange in this picture, and it's a simple task to connect a digital component to those ports by wires.
Each of these outputs provide about 4omA of current
Simply choose a Digital input or output pin which is not currently used by another device, such as the motors or sensors on the SAMrI robot.
Those pins which are labelled PWM above are suitable for "Pulse Width Modulation" which is an effective way to control the speed or brightness of a device.
Programming:
In your setup() portion of your program, you must declare whether your pin is a digital output or input:
pinMode(PC5, OUTPUT); // This declares PC5 is an output
pinMode(PC6, INPUT); // This declares PC6 is an input
To set that pin's output to a High or Low state:
digitalWrite(PC5, HIGH); // This sets the output to 5V
digitalWrite(PC5, LOW); // This sets the output to 0V
To read a Ports input to a variable such as my_port_state:
my_port_state = digitalRead(PC6); // This reads the digital port state into your variable my_port_state
https://www.arduino.cc/reference/en/language/functions/digital-io/pinmode/
https://www.arduino.cc/reference/en/language/functions/digital-io/digitalread/
https://www.arduino.cc/reference/en/language/functions/digital-io/digitalread/
Sample program for flashing light on PC5:
void setup() {
pinMode(PC5, OUTPUT); // Declare PC5 ( port c pin 5 ) an output
}
void loop() {
digitalWrite(PC5, HIGH); // Turn the on LED
delay(1000); // wait one second
digitalWrite(PC5, LOW); // Turn off LED
delay(1000); // wait one second
}
Writing to a port
You can write to a port directly so all bits on that port are changed simultaneously. This technique is valuable if you need an instantaneous change of several bit simulaneously and don't mind the increase in complexity of reading the code.
In Setup, you can change the direction of each port bit with the Data Direction Register Definition instruction:
DDRD = B11111000; // sets PD0, PD1, and PD2 as outputs, and the PD3-PD7 as inputs
In the program, you can modify the entire ports in one instruction. This short code fragment alternates every other bit on and off every second.
PORTD = B01010101;
delay(1000);
PORTD = B10101010;
delay(1000);
Visit https://www.arduino.cc/en/Reference/PortManipulation for more information.