Part 7 - Prototyping
Enough talk, let's build something. In this section we'll develop a prototype with a screen, some sensors, a button and some LEDs. The sensors (except for the analog light sensor) are a selection from my current tricorder. More sensors and buttons can be added later, but we'll start small, and we'll use all the pin modes and bus types except serial tx/rx. We'll also upload a library and run some code to verify that everything's working.
Things we'll use (read through this entire section before ordering anything):
-an Arduino Uno R3 and USB cable (any supplier-Ebay is fine)
-a 1.8" TFT display with a ST7735R controller (https://www.adafruit.com/product/358 or similar from Ebay), and a 100-150 Ohm resistor
-the following I2C sensors: BMP180 (Temp/pressure), GY21 (humidity), GY271 or GY273 (Magnetometer), MLX90614 (IR temp - raw or board-mounted). Get these from Ebay. It's OK if you don't have them all before you begin, as Arduino code still runs if hardware is missing
-an Adafruit ALS-PT19 analog light sensor (https://www.adafruit.com/product/2748 or see below).
-(1-3) momentary SPST push switches, and 5k ohm resistors. Any switch will do so long as it can be connected to the protoboard.
-(3) radial pin LEDs and (3) 100 ohm resistors (other values may be used depending on LED brightness)
-a proto-board with two bus strips on each side
-proto-board jumper cables: male-male, male-female
-wire (#24-28 insulated wire, or #28 enameled magnet wire)
-recommended: some header pins pulled from a male header strip
-optional: hook jumpers if you don't want to solder some things just yet
-optional: 9v battery, connector, and 2.1mm barrel plug - if you want try battery operation
-a low wattage fine-tipped soldering iron and solder (flux pen optional but recommended)
-the Arduino IDE (installed on your computer)
[Caution: I'm sure that most of you have experience soldering and working with circuits. If not, there are a couple of common-sense precautions you should take. Don't solder a circuit that's powered - you can easily ruin a board that way, or ruin yourself. Be safe and unplug power adapters and USB cables first. Arduinos run at low voltage/amperage and are not inherently dangerous, but when connected to anything that's plugged into an AC outlet, they become part of your house's electrical system. Do not touch another electrical device when you're touching an AC-connected Arduino. You will not reach Sto'Vo'Kor if you die in a soldering accident. Your death will have no honor.]
0) As we go along, start thinking about how and where in your tricorder you will eventually mount all these components, and as you're soldering, make the leads long enough for your eventual build. Solder once, not twice.
1) We'll set up our protyping environment by connecting the following pins on the Arduino to the protoboard:
a) 5v to the red top proto strip (+ strip)
b) Gnd to the blue top proto strip (- strip)
c) A4 to the red bottom proto strip (SDA strip)
d) A5 to the blue bottom proto strip (SCL strip)
2) Wiring the SPI display - Using male-female jumpers, make the following connections:
a) CS or TFT_CS to Uno D10 (Mega/Neduino D53)
b) RES or Reset to Uno RES pin
c) DC or AO to Uno D8 (Mega/Neduino D49)
d) SDA or MOSI to Uno D11 (Mega2560/Meduino D) ... check Mega pin # ...
e) SCL or SCK to Uno D13 (Mega2560/Meduino D) ... check Mega pin # ...
f) GND to the '-' strip
g) Vcc to the '+' strip
g) BkLigt or Led ot Lite to any unused protoboard row
h) 100-150 ohm resistor from that protoboard row to '+' strip
3) Load the TFT library - from https://github.com/adafruit/Adafruit-ST7735-Library, download the file Adafruit-ST7735-Library-master.zip into any directory. From the IDE's Sketch/Include Library/Add *.zip menu item, locate and load this .zip file. The TFT library is now loaded, and barring updates you will never have to do this again. Repeat this process for the Adafruit GFX library located at https://github.com/adafruit/Adafruit-GFX-Library
4) Test the display - Plug in the USB cable to your computer and your Uno. Under the IDE's File-Examples menu, look for the Adafruit_ST7735 item and hover over it. Find and open the program 'graphicstest'. Near the top of the code, find and change (if necessary) the following pin selections:
#define TFT_CS 10
#define TFT_RST 0
#define TFT_DC 8
Make sure this line does not begin with a '//':
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST); //1.8 display
Now, under the Tools menu, make sure 'Arduino Uno' is selected. Also, make sure there is a selected COM port (you will probably have only one choice). Press the 'Upload' button on the IDE's tool bar and in about 10 seconds your display should begin running the demo code. If it fails, go back and check your wiring and the pin #defines in your code. If the code upload fails, try a different COM port.
5) Wiring I2C sensors - If you're soldering, connect four wires of appropriate length to the first I2C sensor board. It may also be handy to solder header pins to the other ends of the wires - this makes a better connection to the protoboard.
Now, connect as follows:
a) + or 5v or VCC to the '+' strip
b) - or Gnd or Vdd to the '-' strip
c) SCA to the SCA strip
d) SDL to the SDL strip
Wire up and connect all the other I2C sensors in the same way - they all share the same four strips. Regarding the MLX90614, check for the pinout in the manufacturer's datasheet online - it's simple, but make sure if you're supposed to be looking from the top or bottom - or buy it pre-mounted on a board.
5) Testing the I2C sensors - From https://gist.github.com/tfeldmann/5411375, download the program 'i2c_scanner.ino'. One Arduino annoyance is that each program must reside in an identically named folder, so make sure the unzipped file resides in a directory named 'i2c_scanner' (cases must match). Open this program in the IDE and upload to the board. Now from the Tools menu, open the Serial Monitor. Every few seconds it should refresh with address of each I2C device found. If you see gibberish instead, make sure your speed is 9600 baud (lower right). Count the listed devices and make sure you have them all (one per I2C address); otherwise, check your wiring.
These sensors need libraries to return useful data, but we'll deal with that in the next section.
6) Wiring the analog light sensor - I chose an inexpensive analog light sensor to illustrate the use of an analog input pin. You could really use just about any analog light sensor (even UV or IR) that runs on 5v and has only three pins - +,-,and Out. Connect:
a) + to the '+' strip
b) - to the '-' strip
c) Out to pin A0
7) Testing the light sensor - under File/Examples/01.Basics, open the program 'AnalogReadSerial' and upload it to your board. Now open the Serial Monitor as before and try covering and uncovering the sensor. Note the changing values from 0-1023. Analog sensors generally don't require libraries.
8) Wiring a button switch - You can control the tricorder using 1, 2, or 3 buttons. They are connected to the Uno using digital pins as inputs. You only need one for the prototype, but feel free to add two more to D3 and D4 if you want - I'll post 1 and 3 button versions of the code. Connect the switch to two free protoboard rows and:
a) connect a 5k ohm resistor from the '-' strip to one side of the switch
b) connect a jumper from the resistor/switch junction to pin D2
c) connect a jumper from the '+' strip to the other side of the switch
The basic idea here is that the resistor will 'pull' pin D2 to its LOW (0v) state until you press the button, which will then deliver 5v to D2 and pull it to its HIGH state.
10) Testing the switch - Under File/Examples/01.Basics, open the program 'DigitalReadSerial' and upload it to your board. Now open the Serial Monitor and observe the result when you press and release the switch. If you hooked up more than one switch, change the pin number in this program line 'int pushButton = 2;' to 3 or 4.
11) LEDs using Digital Pins as outputs - We'll hook up one to three LEDs to digital pins. Each will need a current limiting resistor, which prevents the LED from drawing too much current from the Arduino, damaging either or both. The longer leg of the LED is the '+' and the short leg the '-'. Connect:
a) the '-' LED leg to the '-' strip. The '+' leg goes in a free row
b) a 100 ohm resistor from the '+' LED leg to an adjacent free row
c) a jumper from the unconnected resistor end to Uno pin D5
If you're connecting all three, use pins D6 and D7 for the remaining two.
12) Testing the LEDs - Under File/Examples/01.Basics, open the program 'Blink'. Change the pin number in the program line 'pinMode(13, OUTPUT);' from 13 to 5 (or 6 or 7). Do the same to the remaining two lines that have pin 13 specified. Upload it to your board and the specified LED should blink on and off.
13) 9v battery (optional) - if you want to give battery powering a try, solder a 2.1mm barrel connector to a 9v battery connector, making sure the center is + and the barrel is -. The Arduino has a built in voltage regulator that will change the 9v to the required 5v. An alkaline battery should last about 4-5 hours. We will explore more battery options later. Meanwhile, reload the TFT test code, then unplug the USB cable and plug in the battery.
We have now prototyped an example of every type of connection and device a tricorder needs. In the next section, we'll upload the Tricorder code and bring the monster to life!