Welcome to RobotGeek 101 : Digital Output Reference. This reference expands upon the concepts covered in the Digital Output Introduction, and introduces more advanced concepts that will give you a more comprehensive knowledge of the workings of Digital Output.
Parts used in this lesson:
These parts and everything you need to complete chapters 1-7 in RobotGeek 101 are available in the RobotGeek Basic Experimenter's Kit.
File > Sketchbook > RobotGeek sketches > RG101 > 01 - digitalOutput > D-blinkWithConstVariables
This sketch looks a lot like
C-blinkWithVariables but we've made a modification to our variables.
const int DIGITAL_OUT_PIN = 13; const int DELAY_TIME = 1000;
In front of each variable you now see the keyword
const. This keyword modifies the variable to be a 'constant variable'. This means that the variable cannot be changed after it is initially created.
Normally variables are stored in the Geekduino's SRAM (much like the RAM on your computer). The Geekduino only has 2 KB of SRAM - on big sketches it is possible to use up all the SRAM and crash the Geekduino. But by using the
const keyword, we tell the Geekduino that the variable should be stored in the much larger Flash Memory (32KB).
In this sketch, we're not going to see any performance improvement by using the
const keyword, but it's a good habit to have as storing contant variables correctly can make a difference in larger sketches.
Keep in mind that the
const keyword should only be used on variables that you know are always going to stay the same from start to finish. Trying to use
const on variables that need to change will cause errors.
File > Sketchbook > RobotGeek sketches > RG101 > 01 - digitalOutput > E-blinkWithDefines
Generally the use of the
#define directive is discouraged, however it's good to understand when you encounter it.
#define can be used much like the
const keyword, allowing you to store data in flash memory instead of SRAM
File > Sketchbook > RobotGeek sketches > RG101 > 01 - digitalOutput > F-blinkWithoutDelay
B-blinkBasic example, we make heavy use of the
delay() function. This can cause issues when we start to integrate the blink code with other features - the
delay() function tells the Arduino to do nothing - so generally speaking the Arduino stops processing code while it is running a
delay() function . This means that the Geekduino cannot accept input or generate output while running
This sketch will show you how to blink an LED at a fixed interval without using any delays. This is accomplished using the
millis() function to monitor the last time the LED was changed. Whenever a certain number of milliseconds has passed, the LED is changed and the timer is updated.
This code example isn't limited to just blinking an LED - you can use this code to run any kind of event at a fixed time, whether you want to update a display, sound a buzzer, or update a counter.
On caveat of this process is that the timing may not be exact - if you have other code that takes longer than the interval time to complete, the LED will blink at a slower rate than expected.
Special Pins / Pins to Avoid
The Arduino environment is highly customizable, allowing you to change pins very easily However there are some pins on the Arduino/Geekduino that serve special purposes
Which Pins: Digital 0 & 1
Why These Pins Are Special:
1 are tied to the Geekduino's Hardware Serial Port. This port is used to program the Arduino as well as communicate to Serial devices (like your Serial Monitor). Attaching devices to these pins may cause problems during programming, or while using serial devices like XBees. Pin 0/1 are tied to the 4-pin UART port on the Geekduino sensor Shield.
When To Use These Pins: Use these pins if you need to use the Geekduino's Hardware Serial Port to communicate with other hardware like XBees, Serial Displays, or other Geekduinos/Arduinos. You can also use these pins for general I/O though you may need to unplug these devices while programming.
When To Avoid These Pins: It will make your life much easier to avoid these pins unless you specifically need them.
Work-Arounds: If you just need general digital pins, try another digital pin, or even an analog pin (see the next Pro Tip). If you need the actually serial port, you might be able to work with a Software Serial library to create a virtual serial port on another pin - keep in mind that a software serial port will not be as efficient / reliable as a hardware serial port.
If you are having trouble programming the Geekduino while you have devices attached, you can also unplug any devices you have attached to pin 0/1 while programming. You can also use an ISP programmer to program the Geekduino without the serial port.
Which Pins: Digital 3, 5, 6, 9, 10 & 11
Why These Pins Are Special: In a future lesson, we’ll explain Pulsed Width Modulation(PWM). Briefly, PWM is a way to approximate “analog output’ allowing you to fade LEDs and control the speed of motors. The Geekduino has 6 pins that are capable of PWM - 3, 5, 4, 9, 10, 11)
There is no harm in using a PWM pin as a normal digital output pin - the pin will act the same as any other digital output. Just keep in mind that if you need a PWM pin later, you may need to re-arrange your pins.
When To Use These Pins: Use these pins if you need do not need PWM or Servo features for your project
When To Avoid These Pins: If you plan on needing PWM or Servo features for your project, you should save these pins and use other Digital I/O pins
Work-Arounds: There are software PWM libraries available that can approximate PWM behavior. You can also add on external PWM hardware to your Geekduino.
Which Pins: Analog 4 & 5
Why These Pins Are Special: These pins can be used for the I2C bus - a data communication method that can allow you to chain several devices to your Arduino.
When To Use These Pins: Use these pins if you need do not need I2C features for your project
When To Avoid These Pins: If you plan on needing I2C features for your project, you should save these pins and use other Digital I/O pins
Work-Arounds: If you want to use I2C hardware with your Geekduino, Analog pins 4 & 5 need to be unused by non I2C devices.
Which Pins: Digital 2 & 3
Why These Pins Are Special: Pins 2 and 3 are ‘Interrupt Pins’. These pins can be configured to halt or 'interrupt' the Geekduino's currently running code. This can be useful when you have an input that is timing critical and absolutely needs to run immediately when the input in enabled, regardless of the current running code.
Most sketches do not use Interrupt pins. Some hardware like SD card interfaces need interrupt pins to work correctly.
When To Use These Pins: Use these pins if you don't need interrupt features.
When To Avoid These Pins: Avoid these pins if you need to use interrupt features for your sketch.
Work-Arounds:It is possible to use pin change interrupts to read changes on multiple pins.
Analog Pins as Digital Pins
|Analog Pin (Hardware)||Digital Pin(In the Arduino IDE)|
The Geekduino's Pins can be accessed at a much lower level than using
digitalWrite() . This method is called port manipulation. This method can be more difficult to use, but can offer superior speeds. Port Manipulation is an advanced topic for users who need to get every last bit of speed out of their Arduino. You can learn more here
File -> Sketchbook -> RobotGeek 101 -> 1-DigitalOutput -> 1-Blink_Basic