This series of articles are the study notes of "An Arduino platform and C Programming", by Prof. Harris, Department of Computer Science, University of California, Irvine. This article is the notes of week 3, Arduino Programs, lessen 3.
3. Lesson 3
3.1 Lecture3-1: Input andOutpu
We've been talking about pins and how they can be inputs and outputs, and be digital and analog. And so now, we're going to talk about how you do that from the code. So what code do you need to write, what function calls do you need to make in order to use the pins as inputs and outputs, in order to use them as digital and analog.3.1.1 Input/Output (1/O)
(1) These functions allow access to the pins
void pinMdoe(pin,mode);(2) Sets a pin to act as either an input or an output
Some of the Pins can be inputs or outputs. Not all the pins, some of the pins can't be inputs, or can't be outputs. For instance, the analog pins. Analog pins, they can be inputs but they can't be outputs, but the standard digital pins, they can be input and output. So, before you use the pins, you have to select if the pin is going to act as an input or an output. And you should do this for all the pins that you're going to use. Before you try to read from it or write to it, you have to assign it to be an input or an output.Now if you don't do this, something will happen. It will have a default value. So, by default, the pin will either be an input or output, and you should never rely on the default value.
Before you access the pins, you should call this function, this pinMode function, in order to assign the pin to either an input or output. It's a library function. It takes two arguments, pin and mode. The job of this is to set a pin to be either an input or an output.
Pin is the number of the pin
- 0 - 13 for the digital pins
- A0 - A5 for the analog pins
- INPUT, OUTPUT or INPUT_PULLPUT
- INPUT_PULLPUT acts as input with reversed polarity
3.1.2 Digital Input
int digitalRead (Pin)- Returns the state of an input pin
- Returns either LOW (0 volts) or HIGH (5 volts)
int pinval;
pinval = digitalRead(3);
- pinval is set to the state o digital pin 3.
3.1.3 Digital Output
void digitalWrite(pin, value);- Assigns the state of an output pin
- Assigns either LOW (0 volts) or HIGH (5 volts)
digitalWrite (3,HIGH);
- Digital pin 3 is set HIGH (5 volts)
3.1.4 Analog Input
int analogRead (Pin)- Returns the state of an analog input pin
- Returns integer from 0 to 1023
- 0 for 0 volts, 1023 for 5 volts
int pinval;
pinval = analogRead (A3);
- Pin must be an analog pin.
3.2 Lecture3-2: Blink Example
in this set of slides, we'll talk about the blink example, a piece of code, a very simple piece of code that everybody needs to run on their Arduino to sanity check it, to make sure everything's working.3.2.1 Delay
void delay(msec);- Pauses the program for milliseconds (ms)
- Useful for human interaction
digitalWrite (3,HIGH);
delay (1000);
digitalWrite (3,LOW);
- Pin 3 is HIGH for 1 second
3.2.2 Blink Example
- Blink is the generic simple example for embedded systems: Like “hello, world”
- File > Examples > 01. Basics > Blink
- Causes an LED to blink
- LED is built-in, no wiring required
(1) the LED connect to pin 13
(2) Blink Sketch
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}