Simple Stick User Guide - Interfacing a LCD with Simple Stick
The LCD interface on the Simple Stick lets you plug in a standard 16 pin LCD and drive it in 4-bit mode. Owing to the size of the Simple Stick and the fact that it has been designed to work on USB power, this LCD interface comes with a few trade offs
The Contrast Pin of the LCD is connected to a fixed resistor, so you will not be able to vary the contrast [Scroll to the bottom of this page to see how to overcome this ;) ]
The Backlight Power Supply uses a 470E resistor (to save current draw from USB) and as a result the back light will be very dim. [Scroll to the bottom of this page to see how to overcome this ;) ]
The Interface uses a fixed I/O for R/W mode select
The Interface is hard wired for 4-bit mode
The Interface works only with LCDs with the following Pin Mapping (commonly available!)
Character LCD - A Quick Overview
Here's an interesting Write up to get you understanding the working of LCDs
http://joshuagalloway.com/lcd.html
To try an LCD place it on the Simple Stick and run the Hello World Example under File->Examples->LiquidCrystal->Hello World, with the following changes
replace this line
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
with
LiquidCrystal lcd(3, 2, 0, 1, 4, 12, 6);
Heres a demo video of this code in action
Heres the code
Simple_LCD.ino
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pinsLiquidCrystal lcd(3, 2, 0, 1, 4, 12, 6);
voidsetup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
voidloop() {
// set the cursor to column 0, line 1// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis()/1000);
}
Hacks to Overcome the LCD Shortfalls
Back light - To increase the intensity of the backlight, you can solder a resistor of lesser value between the 'BL-' pin and the GND pin on your LCD [Video Coming Soon!]
Contrast - You can achieve full contrast by directly grounding the Contrast Pin. Keeping this in mind, if you decrease the resistance value like in the above manner, you can increase the contrast. Remember this time, you need to put in the resistor between the 'Contrast' pin and the GND pin on your LCD. [Video Coming Soon!] Decreasing the contrast is not possible without de-soldering the fixed resistor.
The IR LED on board the Simple Stick lets you generate Remote Control Signals using your Simple Stick. Given the Simple Stick's stick format, this opens up a number of interesting possibilities... " say, you are working on your laptop in a coffee shop.. and you find the program on the television to be boring.. voila! there comes the Simple Stick, program it and zap the channels on the TV ;) " thats the kind of possibilities... (thats just a crazy example! we take no responsibility for the consequences you might end up in as a result of this!) you can take it upto building a 2-way communication system between 2 laptops using Simple Stick!
The IR led is connected to PIN 9 of the Simple Stick which is a PWM pin.
You can use the IRRemote Library [part of the simplestick_demo_code.rar] and program the IR LED to generate remote control signals.
To check, open the IrSendDemo example from under File -> Examples -> IRRemote
Now there are only 2 lines code required,
First creating an IRsend object and then using the IRsendobject.send function
IRsend irsend; // This line creates an IRsend object
irsend.sendSony(0xa90, 12); // This line sends the Power On/Off code of Sony 12bit remote controls
for sending signal using other protocols (NEC, RC5, etc..) the following functions are available
sendNEC(unsigned long data, int nbits) sendSony(unsigned long data, int nbits) sendRC5(unsigned long data, int nbits) sendRC6(unsigned long data, int nbits) sendPanasonic(unsigned int address, unsigned long data) sendJVC(unsigned long data, int nbits, int repeat) sendRaw(unsigned int buf[], int len, int hz)
The IRsend object will automatically use pin 9 on the Simple Stick.
Heres a simple program that sends the sony Power On signal when the push button is being pressed.
IR_Send.ino
#include <IRremote.h>
IRsend irsend;
voidsetup()
{
pinMode(11,INPUT_PULLUP);
}
voidloop() {
if (digitalRead(11)==0) {
for (int i = 0; i < 3; i++) {
irsend.sendSony(0xa90, 12); // Sony TV power codedelay(40);
}
while(digitalRead(11)==0);
delay(100);
}
}
Simple Stick User Guide - Working with TSOP IR Receiver
The
TSOP SM0038 is an IR receiver on the Simple Stick. The TSOP will help you
to interface your TV remote with the Simple Stick and in the Process learn
the basics of Wireless Communication. The TSOP is connected to pin
digital 8.
The
TSOP outputs a constant HIGH signal when idle and as it receives data,
it tends to invert the data. i.e when an IR LED is transmitting data
onto the TSOP, everytime the IR led goes high, the TSOP will go LOW and
vice versa. Remote control signals are often bytes of data that is
encoded and transmitted by pulsing(switching ON & OFF the IR LED at a
specific frequency) Most TV remote controls work at 32-40 Khz frequency
and most receivers can receive this range.
Heres
a link to a nice write up on different remote control protocols. lets
first take a look how the Sony Remote Control Protocol Works. We stick
to Sony as it is the easiest one to get started with. Read this before proceeding
Here's
a basic outline of how the data is sent. Every time you press a button
on a Sony remote control, it sends out a 13Bit data. The first bit is a
start bit indicating there are 12 bits of data following it. The next 7
bits are the command bit which will vary depending upon the keys being
pressed. The last 5 bits are the address bits which will the same for
all buttons but vary for remote controls of different devices.
The
black bars in the following image correspond to high signals (called
marks) and the white spaces in between correspond to low signals (called
spaces). The duration of the 'marks' varies according to the bit being
transmitted. It is 2.4ms for the start bit, 1.2ms for HIGH bit and 0.6ms
for LOW bit. The duration of the 'spaces' is a constant 0.6ms. Every
mark is followed by a space. Any data can be converted to binary format
and transmitted in this manner. In fact this is the basic form of all
types of serial communication.
Technique
to decode this signal train, would be to constantly monitor the TSOP
pin[Digital 8] for its normal state and the moment it produces a low
signal, measure the duration of the low signal. If the measured duration
of the low signal is around 2ms then measure and store the duration for
the next 12 bits of the incoming data. After storing the data, evaluate
the duration and based on the duration convert the data to decimal /
hexadecimal and use it in your application.
There
is an interesting IR remote library that can help you read different
remotes without any difficulty. It can also generate different remote
signals. It can be used to generate these remote control signals on the IR LED connected to the 9th
pin of the Simple Stick (PWM pin).
How to use Libraries in Arduino - An Overview
To use any library you download, unzip the downloaded file and copy its
contents to the libraries folder inside your arduino directory. You can
check the library by opening the arduino ide and going to Sketch ->
Import Library Option, if your library is in the proper location, it
will show up here. Next if there is an example provided with the library
(it will be inside a folder called example inside the base folder of
the library) it will show up under the libraries name in the
File->Examples Menu. You should reopen Arduino for the library to
show up.
Once you install the IRremote, You can try the example program,
IRrecvDemo. This program will give you a serial output of the HEX code
for each value corresponding to each button on a remote. We will be
using the decimal value in our next program. To get the decimal value,
just do the following modification.
replace this line int RECV_PIN = 11; with int RECV_PIN = 8; and this line Serial.println(results.value, HEX);
with Serial.println(results.value);
Here's a video of a simple project - A remote control interface for our Binary Counter.
Here's the source code for the same
Remote_Binary_Counter.ino
/*
This sketch increases a 3 bit number every time '+' button is pressed and decreases the value when '-' button is pressed on the remote.It shows the output on 3 LEDs in Binary Format
*/
#include <IRremote.h>
int RECV_PIN = 15;
IRrecv irrecv(RECV_PIN);
decode_results results;
int i = 0;
void setup()
{
pinMode(11,OUTPUT); // declare LED pins as output pins
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
irrecv.enableIRIn(); // Start the Remote receiver
Serial.begin(9600);
}
void loop()
{
if (irrecv.decode(&results)) {
Serial.println(results.value);
switch(results.value) // if the '+' button is pressed
{
case 2320:
i=0;
break;// 2320 is the value for '0'
case 16:
i=1;
break;// 16 is the value for '1'
case 2064:
i=2;
break;// 2064 is the value for '2'
case 1040:
i=3;
break;// 1040 is the value for '3'
case 3088:
i=4;
break;// 3088 is the value for '4'
case 528:
i=5;
break;// 528 is the value for '5'
case 2576:
i=6;
break;// 2576 is the value for '6'
case 1552:
i=7;
break;// 1552 is the value for '7'
case 1168: // this is the value for the increment button
if(i<7) // if counter value is less than 7 or 3 bits
i++; // increment counter value
else
i=0;
break;
case 3216: // this is the value for the decrement button
if(i>0) // if counter value is greater than 0 or 3 bits
i--; // decrement counter value
else
i=7; // reset counter to 7
break;
}
int a=i%2; // calculate LSB
int b=i/2 %2; // calculate middle bit
int c=i/4 %2; // calculate MSB
digitalWrite(11,c); // write MSB
digitalWrite(12,b); // write middle bit
digitalWrite(13,a); // write LSB
irrecv.resume(); // Receive the next value
}
}
Simple Stick User Guide - Programming the Push Button
In this tutorial, We'll see how to make use of the push button on the Simple Stick.
The push-button on the Simple Stick is designed to work with the internal pull-up resistor on the
microcontroller. For your understanding, They will give a low signal
when the button is pressed. Enabling the internal pull up on the
microcontroller will keep the corresponding pin HIGH unless the button
is being pressed. When the button is being pressed, the corresponding
pin will go LOW.
The push-buttons is connected to Digital Pin 11
So lets try to write a Simple
Program to glow an LED while a button is being pressed. Then we will
improvise our Binary Counter Program by adding a button to it.
Heres a demo video
Heres the code, the comments are self-explanatory
Simple_Button.ino
/*
This sketch turns on the LED while the button is being pressed
*/
void setup()
{
pinMode(11,INPUT); // Declare the 11th pin as a input pin. The button is on the 11th pin
digitalWrite(11,HIGH); // enable the internal pullup resistor - Everytime you use a switch on the InduinoX, do this
pinMode(13,OUTPUT); // Our LED
}
void loop()
{
while(digitalRead(11)==0) // digitalRead(11) will read the current state of pin number 7 and give an output of '0' or '1'.
//In our case, the digitalRead() funciton will return a '0' when the button is being pressed and '1' when the button is not being pressed
// The Control will stay inside the while loop till the button is released
{
digitalWrite(13,HIGH); // Turn the LED ON
}
digitalWrite(13,LOW); // Turn the LED OFF when the control exits the While loop
}
Now Here's the switch added to the Binary Counter
Button_Binary_Counter.ino
/*
This sketch increases a 3 bit number every time a button is pressed by the user and shows the output on 3 LEDs
*/
int i = 0;
void setup()
{
pinMode(5,OUTPUT); // declare LED pins as output pins
pinMode(10,OUTPUT);
pinMode(13,OUTPUT);
pinMode(11,INPUT);// Declare the 11th pin - Button - as a input pin.
digitalWrite(11,HIGH);
}
void loop()
{
if(digitalRead(11)==0) // if the button is pressed
{
if(i<7) // if counter value is less than 7 or 3 bits
i++; // increment counter value
else
i=0; // reset counter to 0
int a=i%2; // calculate LSB
int b=i/2 %2; // calculate middle bit
int c=i/4 %2; // calculate MSB
digitalWrite(5,c); // write MSB
digitalWrite(10,b); // write middle bit
digitalWrite(13,a); // write LSB
while(digitalRead(11)==0); // wait till button is released to avoid incrementing the counter again
delay(100); // small delay to avoid debounce
}
}
Simple Stick User Guide - Building a 3-Bit Binary Counter
Ensure you have gone through our Simple Stick Overview Page before you go ahead with the Programming Tutorials.
A Binary Counter is an interesting project for the beginner to get
started right after blinking an LED. A Binary counter is a simple
counter that displays the current counter value in binary format. we
have 3 On-board LEDs connected to Pins 5,10 &13 on the Simple Stick, we
can use these to build a 3-bit binary counter that will automati cally
increment every second and reset once it reaches 7.
We can assume the
LED connected to PIN 13 to be the LSB [Least Significant Bit] and the
LED connected to PIN 5 as the MSB [Most Significant Bit] . We can have a
counter variable that we can increment till 7 and reset to 0 on
reaching 7.
Now Lets take a look at the logic
Heres how a Binary counter will work
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
0 0 0
...... and on
If you notice, the LSB is toggling every cycle, the Middle Bit is
toggling every 2 cycles and the MSB is toggling every cycle, To achieve
this effect, we can use the MOD operator (%)
Counter % 2 will produce an alternating output of '1' & '0' that changes every cycle
(Counter/2) % 2 will produce an alternating output of '1' & '0' that changes every 2 cycles(whatever you divide by!)
(Counter/4) % 2 will produce an alternating output of '1' & '0' that changes every 4 cycles
Heres a Demo Video of the same
Heres the code for this...
Binary_Counter.ino
/*
This sketch automatically increases a 3 bit number every second
and shows the output on 3 LEDs
*/
void setup()
{
pinMode(5,OUTPUT); // declare LED pins as output pins
pinMode(10,OUTPUT);
pinMode(13,OUTPUT);
}
void loop()
{
for(int i=0;i<8;i++) // increment automatically from 0 to 7
{
int a=i%2; // calculate LSB
int b=i/2 %2; // calculate middle bit
int c=i/4 %2; // calculate MSB
digitalWrite(5,c); // write MSB
digitalWrite(10,b); // write middle bit
digitalWrite(13,a); // write LSB
delay(1000); // wait for a second
}
}
Enjoy... and feel free to drop us an email with questions you might have -> info@simplelabs.co.in
Unzip the downloaded file and remember its location
Connect the Simple Stick to your PC using the usb cable provided.
You should see the power (red) led go on on the Simple Stick.
Windows will detect a new usb device. Ignore any options to install
drivers and Do not let windows install any drivers automatically.
Open windows device manager
You can open windows device manager by right clicking on 'My
Computer' and choosing Properties. In the new Window, Click on Device
Manager [In Windows XP, this can be found under 'Hardware Tab' of the
Properties Window]
Now Scroll Down the List of Devices in the Device Manager till you
come to a Device 'Arduino Leonardo'. You should see a warning icon next
to it as the drives have not been installed. Right Click on this Device
and Choose 'Update Driver Software...' option.
Now, Windows will provide you two options, Choose the one that says
“Locate and install drivers manually from a specific Location.”
In the next screen, you will see a browse button, click on the
button and browse to the location of the drivers folder[You will find
the drivers folder inside the unzipped arduino folder] and then click
'OK'. Then Click 'Next'
The Device will be installed as a 'Arduino Leonardo (COMXX)', You should be able to see a new device 'Arduino Leonardo (COMXX)'. Make a note of this Number.
Now go the unzipped Arduino folder and run the arduino.exe file
The Arduino IDE will open up
In the Arduino IDE open the 'blink' example program by clicking on it from under File -> Examples -> Basics
Now select the board by going to Tools -> Boards -> and
selecting 'Arduino Leonardo'
Now select the Serial Port by going to Tools -> Serial Port ->
and selecting the 'COM port that was earlier shown in the device
manager'
Now Click on the Upload button. In a moment, you should see 2 LEDs
Rx & Tx blink on your Simple Stick indicating the program being
uploaded. Once the program is uploaded, you will see the White LED[13th Pin] on the Simple Stick Blink