loading

So why's this here?

As a serious hobbyist I've spent a good deal of time researching the details of using peripheral devices of one kind or another in micro controller projects. The information is scattered, often incomplete and in most cases deal with a single device. After completing my last project I realized I had an excellent tutorial platform that brought several major hobbyist hurdles together into one place.

This project will take some effort but in the end you will have a stand alone micro controller platform that includes an LCD screen, IC2 Keypad, EEPROM memory read/write and FTDI in-circuit programming. You can upload code by connecting your project directly to a computer using a 5V programming cable.

There are two sketches (working code) included here. The one titled KPD_LCD_tutorial is a large sketch showing the devices working in concert. You will see how LCD prompts and other audio visual cues can guide a user through a keypad programming sequence. Code also includes writing and reading EEPROM memory, automatic LCD brightness, beeping keypad buttons, audio/visual feedback and other ways to make a device smarter.

The other called KPD_LCD_ basic is just the bare bones code required to give you a functioning keypad, LCD, user feedback routines and EEPROM storage/recall of data.

The project is not rocket science. Just reproduce the circuit diagram on a breadboard, plug in a 5V FTDI programming cable and upload the sketch. By following along in the sketch comments you will soon get an idea of whats happening and hopefully see how you might adapt the process to your own ends.

Skills and tools needed: You need rudimentary soldering skills and of course a soldering iron of some sort. If you want my suggestion for a soldering iron go with the Hakko FX-888D available in Canada from SolarBotics. This is a great iron (it barely gets a rest in my little lab).

If you follow the schematic and directions you only need a basic understanding of electronics however I strongly suggest you research anything you don't. It's the only way to progress.

Finally you need tenacity and by far that's the greatest asset anyone interested in this stuff can have. Don't give up.

Step 1: Gathering Your Parts.

The Bill OF Materials

The schematic notes the values of all parts (DigiKey is a very useful source). Everything should be available in through hole packages (conventional wire leads and pins). For bread boarding a pin pitch of .01" works well.

Part numbers and links to suppliers are shown for most but not all parts since things like resistors and capacitors are easily sourced.

5- .1uF CERAMIC CAPACITOR - (DigiKey BC1084CT-ND)

2 - 10uF ALUMINUM CAPACITOR (DigiKey 493-12772-1-ND)

1 - 4.7uF ALUMINUM CAPACITOR (DigiKey 493-12781-3-ND)

2 - 22pf CAPACITOR (Sparkfun 22 pf cap)

1 - 1N914 DO35-4 Fast Switching Diode (DigiKey 1N914ATR-ND)

1 - Tri colored RGY LED 5mm Common Cathode (DigiKey 67-2184-ND) - or 3 separate LED's with resistors

1 - R-78C5.0-1.0 DC/DC-Converter 1.0 AMP SIP3 Single Output (DigiKey 945-1395-5-ND) - or equivalent.

1 - MEGA328-P DIL28-3 MICROCONTROLLER (Sparkfun Mega328) - (With boot loader)

1 - DIL16 I2C-Bus Remote 8-bit I/O expander PCF8574PN (DigiKey 568-1075-5-ND)

1 - TC7HC14 CMOS Schmitt Inverter DIP14 (DigiKey TC74HC14APF-ND)

  • Note: This chip actually has 6 inverters in it. You will only use one of them.

1 - Breakaway section of 40 header pins (Sparkfun .1" headers)

1 - Yellow 3mm LED (DigiKey 511-1265-ND)

1 - Red 3mm LED (DigiKey 511-1255-3-ND)

1 - Red 5mm LED (DigiKey 67-1105-ND)

1 - Green 5mm LED (DigiKey 67-1101-ND)

1 - AT-2440 PIEZO Transducer (Digi Key part # 668-1103-ND)

1 - Q1 16MHz XTAL/S QS CRYSTAL (Sparkfun Crystal)

1 - Q2 BC547B TO92 NPN Transistor - or 2N3904 or 2N2222. Many gen purpose NPN's will work

8 - 10K RESISTOR - Note: All resistors are 1/4 watt, carbon film, +/- 5%, Through Hole types (DigiKey)

1 - 100R RESISTOR

2 - 1K RESISTOR

3 - 560R RESISTOR

2 - 150R RESISTOR

2 - KSA-TACTILE-SWITCH KSA Series(Sparkfun momentary ON tactile switch) - or any momentary switch

1 - Miniature SPST switch - or any on/off switch

1 - 8 Ohm speaker. (Suggested 100 - 200 mW miniature style however any 8 Ohm speaker will do)

1 - Key Pad 4X3 (Sparkfun KPD)

1 - Serial enabled LCD (Sparkfun 16X2 white on black LCD)

1 - Wire harness for the LCD (Sparkfun sensor harness)

1 - 5V FTDI Programming cable (Sparkfun FTDI cable) - Note: Must be a 5V version.

Some additional notes on parts;

  • You will need some way to wire up the circuit on the breadboard. One useful method is a wire kit like this from Sparkfun and/or a set of 30 jumper wires also from Sparkfun. Its cheap, convenient and they last forever. Stranded wires (unless tinned with solder) do not work well with breadboards.
  • The breakaway strip of .01" header pins will need to be broken into a set of 6 pins for the FTDI header and another strip of 7 pins for the keypad. The rest can be used as test and hookup points as you see fit.
  • You'll need a fair sized breadboard to fit it all. This is the one that gets the most use in my own lab
  • Finally, the suppliers listed are only to help you find things and are by no means the only sources. Heck if you're handy with a soldering iron you can scavenge a lot of the parts from junk circuit boards

Step 2: Understanding the Schematic

Circuit Description

I've tried to simplify things to keep the BOM down. For example, the original circuit was designed to use a pair of X-Bee wireless transceivers however ultimately its simply a changing pin state that affects the program and this can be done with a simple momentary switch and a resistor which simulates using a remote.

Your breadboard will need a supply. If you have a bench power supply dial it in for 8-12V and limit the current output to about .8 amps if you can (just in case something gets wired up wrong). If you don't have a bench supply then consider a dedicated breadboard power supply (available from Sparkfun or SolarBotics). If neither is available then a simple 9V battery will work for a while.

Avoid powering the board directly from wall wart style power supplies like a plug in cell charger since they are often not sufficiently isolated from mains AC power.

Note: The Mega328 and LCD both need a clean source of 5V so if you use a 9V battery you should still use a DC/DC regulator of some sort

It's best to download and print the PDF file containing the schematic. Use that as a reference while reading the circuit description. Note that the connectors in the circuit diagram (K1, K2 etc.) are not necessary for the bread board and are not included in the bill of materials.

Voltage regulation

Circuit power is introduced to pin 1 of regulator DC1 via K7 . The regulator is a 5V /1A switching DC/DC converter from Recom (R-78C5.0 available from DigiKey). The input supply to the converter shown here is 12V however an input voltage range from 8-28 volts is allowable. These converters operate at 91% efficiency, they run cool and will plug easily into a bread board. The critical voltage is the regulated 5V ouput. Power ON indication is provided by red LED4.

Processing

At the heart of our venture is, of course the stand alone MEGA328P with boot loader. (This MCU is called a stand alone because you're plugging it into a circuit built specifically to support it) . Yellow LED1 and R7 attached to SCK (pin 19) are there to simply help you know when the chip is active. It will flash momentarily when the chip is reset or communicating during an upload. That particular part of the circuit is not strictly required for stand alone operation of the MCU but its a nice feature. The 16MHz crystal, Q1, and 22pf capacitors on pins 9 and 10 provide an oscillator. You could use the chips internal 8MHz oscillator and bare bones this even more but I don't recommend it. Proper operation of the sketch is based on accurate timing and 8MHZ just doesn't cut it (I tried).

Note: When you order your Mega328 make sure it comes with a boot loader installed unless you know how to do it yourself.

Debounce

The AtMega328 lives in a digital world of ones and zeros with no gray areas. What this means 99 percent of the time is that an input or output pin must be read as either HIGH or LOW for code to work properly. In our case the program is constantly checking the HIGH/LOW state of pin 13 which is controlled by a switch connected to header K1.

Unfortunately every switch will 'bounce' when thrown which means the signal oscillates rapidly on and off until it settles at its new state. This doesn't sit well with a processor looping through code in milliseconds since it could witness dozens of HIGH/LOW states every time the switch is thrown.

The debounce circuit makes absolutely certain pin 13 sees a stable HIGH or LOW signal with no oscillating between states. How this works is explained below.

To ensure a known state exists until we trigger a change in some manner we pull pins HIGH or LOW. Pulling a pin up or down isn't always enough however if your trigger pulse isn't stable and switches alone definitely do not deliver a stable trigger pulse. Resistor R15 keeps pin 13 at ground potential, a digital LOW, to ensure it never goes high from some stray effect. The input pin on Schmitt inverter IC3 (pin 1) is kept in a HIGH state via resistor R2 until the switch connected at K1 and resistor R3 provide a path to ground which pulls pin 1 LOW. When that happens the inverter, sensing a LOW on pin 1, outputs the opposite on pin 2 (hence the name inverter). MCU pin 13 gets hit with 5 volts but only uA of current with no oscillation of the signal.

Capacitor C1 provides a smoother transition between HIGH/LOW states.

Note: The TC7HC14 (IC3) actually contains 6 individual inverters. We will only use one however the rest can be used later on to provide clean, positive triggers as you turn this project to your own ends.

Audio/Visual feedback

Audio and visual feedback to the user is provided during the LCD prompted programming sequence by a single tri-color (RGY) LED connected to pins 5, 6 and 14 and a transistor controlled piezo at pin 12 of the MCU. There's nothing like a 'that's wrong' sounding beep and a flashing red or yellow LED to make you think twice about what you're doing. Conversely a green LED and (happier?) sound lets you know things are progressing nicely. You could use 3 LEDs and resistors rather than the RGY but why increase the parts count?

An 8 Ohm speaker connected at pin 11 of the MCU provides audible indications during program operation. Generated tones announce program stages such as startup, color switching and program end.

Port Expander and keypad

A 12 button 4X3 keypad connected to JP1 is wired to chip IC2 on the schematic. The PCF8574P Port Expander is a I2C (Two Wire Interface) device which lets us control a 7 pin keypad with only 2 pins of the MCU namely SDA and SCL (MCU pins 27 and 28) .

Notice the chips address (0x20) written on the schematic. This address is assigned by grounding pins A0, A1 and A2. Connecting these specific pins to ground is a must since the address of the device has to match the address in the sketch or the controller will never find it.

Note that pins 4, 6 and 9 of the port expander are pulled up with 10K resistors R4, R5 and R6.

In-Circuit programming

In-Circuit programming of our controller is accomplished through FTDI header J1 and a 5V FTDI programming cable. This is a very reliable method of uploading code from the Arduino IDE however be aware that an FTDI cable is more than just wires. It is a USB to Serial converter with it's own built in processor (take care hooking it up).

Notice that FTDI header J1 has a 5V pin (labeled VCC in the schematic). This is an output pin so that the programming cable can supply power to your MCU for the upload. Make sure you disconnect any other power source before hooking up the cable. All the magic of FTDI uploading to the chip will happen on pins 2 (RXD) and 4 (TXD). The boot loader gets a 'standby for upload' signal when MCU pin 1 (RESET) gets pulled to ground momentarily by DTR.

  • NOTE: It's always a good idea to disconnect the LCD from the board during an upload to prevent any corruption of the serial LCD itself.
  • If LED 1 is included in your circuit you will see it flash rapidly as the controller communicates with your coding IDE during the upload process.

Remote Simulator

On the schematic you can see that pin 17 on the MCU is called ORIDE. When the voltage on the ORIDE pin goes momentarily HIGH it's interpreted by code as a pause signal which halts all operations. The next time it goes HIGH it's treated as a resume signal.

To provide a momentary signal we simply attach a 10K resistor to ground on the ORIDE pin and connect it to a momentary push button switch that has 5V connected to the other side side. When you push the button the 5V delivered to ORIDE will provide the HIGH signal needed as a trigger replacing the identical function provided by the wireless remote. When you release the button, the pin immediately (apart from button bounce) goes to ground via the resistor. We have code in the sketch to deal with the bounce.

Liquid Crystal Display

The Serial Enabled LCD is a 2x16 character liquid crystal display that only requires a single pin on the MCU to do its job. This particular unit is complete with it's own processor circuit board. The circuit wiring of LCD header K8 matches the LCD wire harness in the parts list.

That's it for the circuit. It's not overly complicated and provided you wire it correctly it will work very nicely on a bread board which we'll get to in the following steps.

Step 3: Soldering the Key Pad Header Pins

The keypad is the only bit of soldering to accomplish. Create a keypad header by carefully breaking a single 7 pin section off the .01" pitch break away header pins. Place the short ends in the keypad PCB as shown in the photo and solder them into place.

NOTE: This instructable is large enough without getting into soldering techniques etc. If you need help well there's an instructable on how to solder. Go figure.

Step 4: Laying Out the Breadboard

We'll start by laying out our major components on the breadboard. Don't try to crowd things together since you'll need room to run wire, place components and just to get your fingers in. A good quality set of tweezers is a great tool to have for working with parts.

Important: Static electricity can damage or destroy some integrated circuits. Take precautions when handling sensitive parts. Here's a link to get you started.

Place your parts as shown on the photo. The arrows indicate pin 1 of each part. Most chips have a U shaped indentation on top of the body. The pin to the left of this mark is normally pin 1. Other devices may have a small circular indent next to pin 1. Check the data sheet if in doubt.

Note: A breakout header can be made using 8 break away pins. Although it isn't strictly necessary, this header can be used as an easy access point for MCU pins later on.

Note: The header marked LCD is only used if you decide to solder the LCD harness in place.

Step 5: Wiring and Testing the Power Supply

NOTE: THIS IS A DC TO DC CONVERTER. DO NOT APPLY AC VOLTAGE TO THIS DEVICE

Wire the converter as shown in the photo. The long orange wire is connected to the 12V DC power rail at the top. The brown wire from pin 2 is the main DC ground (yes I should have used a different color).

The red wire connects to the positive side of what will be our main 5V bus and the green wire is the bus ground.

Take one of your red LEDs and plug the ANODE (usually the longest wire lead) into the converters pin 3 breadboard track as shown and complete the circuit with a 560 Ohm resistor connecting the cathode to ground. This is our 'Power On' indication.

Finally place a 10uF capacitor across the 5V rail as shown making sure to get the positive and negative leads orientated correctly.

Hook up a 8-12V (recommended) volt DC only power supply to the top rail making sure the positive and negative are correct. If everything is wired correctly your Power On LED will illuminate.

If you have a multi meter, check the output of your DC/DC converter. It should be within millivolts of 5V.

Disconnect the 12V supply.

Step 6: Hook Up the 5 Volt Rails

Create additional 5 volt rails on your board as shown being extra careful to not accidentally connect to the high voltage rail. Test all new rails for the correct voltage.

Step 7: Wiring the AtMega 328 Step 1

Note: You may need to bend the wire leads of some resistors as shown for easy placement.

Make sure you follow the schematic to fully understand the wiring procedure.

We start at pin 1 of the MCU (indicated by the yellow arrow).

  1. Connect a .1uF cap between FTDI header pin 6 (DTR) and MCU pin 1 (RESET). This capacitor is critical to a successful upload because it sends a trigger signal (a momentary LOW signal) to the chips boot loader telling it to look for a pending upload.
  2. Connect the 1N914 diode between 5V and MCU pin 1 making sure the diodes black line is towards the 5 V rail
  3. Connect a 10K pull up resistor from 5V to MCU pin 1.
  4. Run a wire from MCU pin1 to one side of your reset switch.
  5. Connect the other side of the reset switch to ground

Step 8: Wiring the AtMega 328

Complete the wiring as shown in the photo. (Note the LED in the picture is green not yellow simply because I didn't have a yellow one at the time.)

  • Take care to get the FTDI pin 5 (TXD) to MCU pin 3 wire correct.
  • Connect a 10K resistor between MCU pin 2 (RXD) and FTDI header pin 4
  • The 16MHz crystal is connected across pins 9 and 10 and both pins to ground with 22pF caps.

Double check all the wiring you've done against the schematic.

Power your board and press the reset button. You should see the yellow LED blink several times as pin 1 is pulled low.

If it doesn't ;

  • Test for 5V on pin 1. You should see a voltage drop on this pin when the reset button is pressed. If you don't then you have a bad or incorrectly wired reset switch.
  • Test the LED using a 3V button cell (Anode to positive)).
  • Make sure the LED is installed correctly and the resistor value is correct
  • Recheck all wiring for correctness

Step 9: Do Your First FTDI Up Load

IMPORTANT: Remove all power sources to your bread board. The FTDI cable will power the board.

Connect the 5V FTDI programming cable as shown. It does not come with any sort of markings to identify the header pins (which is why I've labeled the ground pin on mine).

Plug the USB end into your computer and you should immediately see the yellow led blink a couple of times.

If you are using the Arduino IDE (Ver 1.8.1) and the 5V programming cable from Sparkfun:

  • Click Tools - Board - and select Arduino/Genuino Uno as the target
  • Click Tools - Programmer and select AVRISP mkII
  • Click Tools - Port - and you may have to select a port if it didn't automatically select

A good first sketch to upload is the simple Arduino Blink sketch.

Note the yellow LED on your circuit blinks rapidly for a few times when the upload commences.

If everything works you should see the writing/reading process in the IDE and you now have a functioning, in-circuit programmable micro controller. Congrats.

Note: At this point in our construction none of the I/O pins are connected detrimentally so it doesn't matter what we upload however always think before uploading a sketch. You need to be aware of how the MCU pins are connected and what that means to the new program.

Important: Since our FTDI header is not soldered in place it tends to come out of the bread board when you try to disconnect the cable. Make sure you reset the header back into the bread board correctly.

Step 10: Connecting the Port Expander to the MCU

Next we set up our PCF8574P port expander.

It's critical to set the chips address correctly. Our code is expecting address 0x20 and in order to do this we ground pins 1, 2 ,3. (Addresses and how to set them are found in each chips data sheet.)

Next we connect our I2C Serial Data Line (SDA) and Serial Clock Line (SCL) between the MCU and the port expander. These are MCU pins 27 and 28 to expander pins 15 and 14 respectively. These are the communication links between the chips.

Place the 10K pull up resistors on pins 4, 6 and 9 making sure the resistors connect to the hot side of the 5V rail and not ground. These resistors provide close to 5 volts while severely limiting the current.

Connect VDD pin 16 to 5V and a .1uF bypass capacitor to ground. Finally connect pin 8 to ground.

Note: capacitors C2 and C3 are bypass capacitors to shunt unwanted AC signals to ground. Bread boards are notorious for picking up noise from nearby AC sources like overhead lighting etc.

Step 11: Connecting the Keypad to the Port Expander

Connect 10K pull up resistors R4, R5 and R6 on pins 4, 6 and 9 of the Port Expander chip. These resistors are connected to the 5V rails.

Note that the resistor on pin 9 in the photo is connected incorrectly (to ground).

Hooking the keypad to the port expander takes 7 wires so we'll do it in two steps to avoid confusion in the photos.

  1. Connect keypad pins 5, 6 and 7 to port expander pins 9, 10 and 11 in that order as shown in the first photo
  2. Connect keypad pins 1, 2 ,3 and 4 to port expander pins 4, 5, 6 and 7 in that order as shown in photo two.

Step 12: Installing Audible Feedback

Piezo

Photo 1: Create a 12V supply for the piezo by running a positive and negative wire as shown to the rail that runs under the keypad. It will work fine with only 9 volts however 12 delivers a nice positive sound.

Photo 2: Next bend the leads of a 1K resistor so it matches the pitch of the piezo's pins. Allow enough lead wire to insert completely into the bread board.

Photo 3: In the clear area below the keypad lay out the piezo circuit as shown. Check the data sheet of whatever NPN transistor you have and place it in the board so the Collector, Base and Emitter leads connect as shown. The one in the photo is a common BC547B. You can use any equivalent NPN type.

Run a wire from from MCU pin 12 to the transistors Base lead.

Photo 4: Finally install the piezo making sure the pins connect across the resistor and the positive pin is connected to the positive wire.

You cannot simply test a transducer piezo by putting a voltage on the base of the resistor. It will only give a faint click. This piezo requires a PWM (pulse width modulated) signal to run. To test it you will need the KPD_LCD_tutorial sketch loaded on your MCU (or some other way to produce a PWM signal such as a signal generator).

Once you have the sketch uploaded and your board powered, you should get a clean beep with each key press.

If you don't, try pushing the reset button and wait a couple of seconds for the MCU to reboot.

Note: MCU pins 13 and 17 are floating (not connected) at this point in our layout. Stray environmental effects or capacitance on the bread board could be interpreted by code as alternating HIGH or LOW pin states. This may cause the piezo to beep intermittently. Installing the 10K pulldown resistors R15 and R16 on these pins will prevent this.

Speaker (Siren)

Run a jumper from pin 11 to a convenient spot on the board and connect the speaker as shown taking care to get the polarity correct on the 4.7 uF coupling capacitor. Just about any small 8 Ohm speaker will do. The one in the picture is a small 100 mW I have around for test purposes. It and has leads soldered to it for the bread board.

Step 13: Providing Visual Feedback

In addition to the piezo's audible feedback a tri-color RGY LED provides some visual cues to the user based on color i.e GREEN for a successful operation, YELLOW for a partial failure and RED for more serious information.

You can see from the schematic that the circuit is designed for a 'common cathode' type LED which means all 3 diodes in the device share a common ground. This allows us to use a single resistor and connect all three anodes directly to the MCU output pins.

Tip: Once you have the LED and its resistor in place you can quickly identify the anode associated with a color by using a jumper wire. Power the board and plug one end of the jumper into a 5V rail and touch the other end to an anode to light up the associated LED.

Testing: To fully test the LED you need the KPD_LCD_tutorial sketch loaded into the MCU. If it is you can test it by entering some known inputs. You'll learn how this works by reading the sketch comments.

Note: The KPD_LCD_tutorial sketch must be uploaded for the following test;

  1. Press the reset button to reboot the chip. Wait a few seconds.
  2. Press 55* - The red LED should flash twice with a low/low tone
  3. Press 100* - The yellow LED should flash once with a low/low tone
  4. Press 1# - The green LED should flash once with a low/high tone

If you get an incorrect color or no sound at any step check your wiring.

Remove bread board power before continuing.

Step 14: Plugging in the LCD

The wire harness from Sparkfun comes with a JST style connector on one end and just bare tinned wires on the other. Looking at the LCD module you'll notice two places to connect the harness, both are electrically identical. You can plug the harness into the LCD and use the tinned wires in your project or solder the tinned wires to the LCD and use the plug at the breadboard.

For our purposes it's simplest to plug the harness into the LCD and use the tinned ends in the breadboard. as shown.

Follow along in the schematic and reference the photo to wire the LCD. When you power up the bread board the LCD factory splash screen will be replaced by "KPD_LCD_tutorial Version 1.0.01" followed by the first program prompt of our sketch. (This assumes the sketch was uploaded.)

To personalize the splash screen follow the comments in the sketch.

Note: To prevent damage always remove power from the breadboard and unplug the LCD module before uploading code to the MCU.

Step 15: Wiring the Schmitt Inverter

Reference the schematic and the photo to wire the inverter and debounce circuit. You can use any style switch provided it is not a momentary type, it must be an on/off switch providing a constant state. This switch is called RUN/STBY with STBY being the switch open position (no path to ground).

The inverter shown in the schematic is 1 of 6 inverters on IC3. We are only using one (connected to pins 1 and 2 of the 14 pin chip) however nothing is stopping you from using the rest in a similar way on any number of MCU pins in your own project later.

Note: Unused inputs on this chip should be tied to either VCC or GND and not left floating. It makes little difference at this point on the bread board since nothing else is connected however make sure to accomplish this in any final circuit design.

Testing: If you have the KPD_LCD_tutorial sketch loaded then testing is simple. Make sure the RUN/STBY switch is in STBY and power the board. Pause for the system to boot then select RUN.

You should hear a double beep and see the red LED flash 3 times every few seconds. This is code advising you that it's received a run command with no parameters entered and so can't proceed. You should also see an LCD prompt asking you to move the switch back to standby. Select STBY and the first LCD programming prompt will appear, it's a smart circuit already.

Step 16: Simulating a Remote Input

Wire the momentary (push button) switch as shown in the photo.

We don't need a debounce circuit here since it's is taken care of in the code. The purpose here is to simulate the input that might come from a remote. Pushing the button simply connects Pin 17 to 5V which the MCU interprets as a HIGH.

The sketch looks at this pin on every loop (many hundreds of times a second) and will enter a pause routine on the first push and leave the pause routine on the second. There is a programmed delay of 7 seconds required between button pushes.

Note: There are literally hundreds of methods to deliver this momentary signal. How about an X-Bee wireless transceiver module or a passive IR sensor?

Step 17: PhotoMOS Simulator

Wire the two Red and Green 5mm LED's as shown. These LED's are so you can see the MCU outputs in action.

The original device using this circuit (with a very similar sketch) controlled AQV251G PhotoMOS relays as seen in the schematic section. These did all the high current work of that device however since optical relays are relatively expensive we simulate MCU control of such devices by replacing them with simple LED circuits. If you want to control a high current device you can simply replace the LEDs with optical relays as shown or by using transistors as switches. Given a properly sized relay you could activate a locomotive sized electric motor with this chip.

Step 18: The Sketches and Program Operation

Both the KPD_LCD_tutorial sketch and KPD_LCD_basic sketch plus the required libraries are included here as Arduino files. The libraries Wire.h and SoftwareSerial.h are already in the Arudino IDE so are not provided.

You will need to have the following libraries installed in the IDE for these sketches to work

  • wire.h
  • SoftwareSerial.h
  • i2ckeypad.h
  • EEPROMVar.h
  • serLCD.h

To get these libraries into the Arduino IDE you must download the zipped files to a folder on your computer then Click on Sketch > Import Library > Add Library. A dialogue box will open up asking you to select the folder which contains the library you want to add. You do not need to unzip the folders, Arduino should do that for you.

Note:The i2ckeypad.h library provided here has been modified to work with Arduino version 1.0 and higher.

To get the sketch, download the file to a folder on your computer and drag it to the Arduino folder on your computer.

Important: The first time you open the sketches in an IDE save them under a new name and only work with the renamed sketch when experimenting with code. This way you can always fall back to a known working version of the sketch if you run into trouble.

Some notes on the sketches:

The tutorial sketch is fairly extensive and will take up a good portion of the chips memory. If you need to add a bunch more you may want to consider the Mega168 as an alternative controller.

The whole idea here is to provide a way for you to quickly integrate an LCD and keypad into your next project. The rest of the code is there to demonstrate this in action and provide an actual function for the circuit itself. If you use a bit of imagination you'll realize that you can do a lot more than blink a couple of LED's with this sketch.

The basic sketch is much simpler and contains the bare code needed to integrate everything in the circuit.

Take the time to read the sketch comments and figure out whats happening. It won't be long before you'll see ways to make it suit your own needs.

Calling:

Understanding how calling works in code will help you greatly when trying to decipher whats happening in the sketches.

The user is pretty much led by the hand when programming via prompts that appear on the LCD. All required inputs are prompted for and errors are handled the same way. Memory is often an issue when programming so you always want to avoid duplicating code. The most common way to do this is by writing the often used code in a function and 'calling' for it when needed. This is how prompting (and a few other operations) are handled here. Rather than have multiple lines that effectively output the same thing to the LCD (as would be the case when handling user input errors) the prompts are written once in a function and called specifically when needed from various points. The program 'jumps' to another place in the sketch and may run many lines of other code before coming back to carry on past the initial calling line.

You will see right away that, when operating, the program is constantly moving around the sketch. To fully understand whats happening you need to (follow the white rabbit?) and move around in the sketch as you read.

Reference:

If you get stuck you may want to use the Arduino help menu. Clicking on the Reference link will answer most questions about the language used in Arduino.

Program Operation:

The actual function of the program is simple. The code simply switches between a blinking green or red LED at intervals determined by the user however you have a great deal of control over how this is done by altering the inputted variables.

Listed below are the parameters which you are prompted to input;

Starting color:

  1. Start Red
  2. Start Green
  3. Random Selection

Switch Time:

  • The number of minutes between color switching or zero for no switching (the blink interval is hard coded)

Start Delay:

  • The length of time in minutes to wait before commencing

Run Time:

  • The total length of time to operate.

Disable Screen?:

  • Indicates wither or not to disable the LCD while running

A siren provides startup, color switch and shutdown alerts. Entry errors or invalid inputs are handled with prompts and appropriate feedback.

Awesome!!! Definitely gonna vote for you :)
<p>Very well done, great first instructable! :)</p>
Thanks. I've tried to make it as clear as possible. Hopefully some will find it useful.

About This Instructable

2,237views

45favorites

License:

More by Copper Dog:Mega328 with LCD, Keypad and FTDI in-circuit programming 
Add instructable to: