Liquid Crystal Display with Keypad for Arduino Part 1

The sketch can be downloaded from my GitHub page here.

The display described here is a two line, sixteen characters per line, display with keypad. This Arduino Shield plugs onto the Arduino, but allows no further access to the Arduino pins. If you want to add other sensors then the display will need to be mounted on a breadboard. These displays often use the HD44780 parallel interface chip. This part describes using the buttons, part 2 will describe using the display.

The buttons:

The buttons are connected to a resistor chain, so there is a different voltage associated with each button, that can be measured by one of the Arduino’s analog pins. The diagram is shown here:

The sketch just reads the button pressed and prints the value out in the Serial Monitor. Plug the shield onto the Arduino and for this sketch only the buttons are working Nothing will be displayed on the LCD. The next post will show how to write to the display.

There may be some difference in the values read for the buttons on your board. These values can be found near the top of the sketch. If the sketch does not produce the correct result then you can see the voltage produced by each button by uncommenting a line in the byte readButtons () sub routine. The correct values can then be used in the variables at the head of the sketch.

The code is based on the tutorial at here.

JYMCU Bluetooth Units, HC05 and HC06 Part 2

Part 1 showed how to set up the Slave Bluetooth HC06 and can be seen here.

The HC05 is a Master/Slave Bluetooth Unit, that at first sight looks identical to the Slave only HC06. I suggest that as soon as you receive the HC05 you mark the board with an M. There are normally six connections on the board, although often only four, Vcc, Gnd, Tx and Rx will have pins. One more connection is required, KEY this is normally connected to ground via a resistor. When this pin is connected to Vcc the unit goes into AT command mode. If this connection does not have a pin then either solder a single pin or a connecting wire.

The HC05 can be powered from either 3 volts or 5 volts, but Tx and Rx should be 3 volts only, so use a Logic Level Converter. The normal baud rate for communications is 9600, but when in the AT mode the baud rate is set at 38400.

To program the HC05 with AT commands first upload the sketch shown below, then remove the power from the Arduino. Connect Vcc to Arduino +5 volts, Gnd to Arduino Gnd. Software Serial is used in the sketch shown here, so connect Tx from the HC05 to pin 2 on the Arduino (Software Serial Rx) and Rx from the HC05 to pin 3 on the Arduino (Software Serial Tx).  Connect the KEY pin to Vcc and apply power to the Arduino.

The HC05 will now be in the AT mode and communication is at 34800. Type AT into the text entry box in the Serial Monitor, which should be set to 9600 baud and the Send CR and LF setting selected. If all is well OK will be displayed. All AT commands must be in UPPERCASE. Try some of the following commands and when you are finished send AT+RESET to exit the AT command mode, connect the KEY pin to Gnd to select communication mode. It is probably best leaving the baud rate at 9600, but you can rename the HC05  to whatever suits you. Of course you need to set the HC05 to be a MASTER by sending AT+ROLE1.

Most useful AT commands are

AT : Ceck the connection.
AT+NAME : See default name
AT+ADDR : see default address
AT+VERSION : See version
AT+UART : See baudrate
AT+ROLE: See role of bt module(1=master/0=slave)
AT+RESET : Reset and exit AT mode
AT+ORGL : Restore factory settings
AT+PSWD: see default password


If you mess things up, or forget what you did then AT+ORGL will reset everything back to factory settings. 

// HC05 Test Sketch - Sending AT Commands
// On first running the Bluetooth module is reaset to factory settings then the speed is set to 9600
// and the name is reset to HC05. The current speed and bluetooth number (MAC) is printed
// Connect RX from the HC05 to pin 2 on the Arduino.
// Connect TX from the HC05 to pin 3 on the Arduino
// to send AT commands remove all power and connect KEY to Vcc
// Switch the power on
// Open up the Serial Port Monitor
// Type in AT and click on the SEND button, OK will be printed in the monitor
// showing that the module is receiving commands.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

String buildString = ""; // Stores response of bluetooth device
// which simply allows \n between each response.

void setup()
// Open serial communications and wait for port to open:
// Hardware Serial port set to 9600
// SoftwareSerial "com port" data rate. 38400.
mySerial.begin(38400); // default speed in AT mode
// reset bluetooth module to factory settings
// Note: the delay blocks are necessary to allow the commands to take place
Serial.println("Initialising ...");
Serial.println("Ensure that both NL and CR are selected");
Serial.println("Enter AT commands");

void loop() {
// Read device output if available.
if (mySerial.available()) {
while(mySerial.available()) { // While there is more to be read, keep reading.
buildString += (char);
buildString = ""; // reset string
// Read user input if available.
if (Serial.available()){


Part 3 will show how to connect two Arduino’s via bluetooth to send data.

TMP36 Temperature Sensor and Arduino

The TMP36 is an analogue sensor, that needs to be read using the Arduino’s analogue input. The sensor outputs a voltage that is proportional to the temperature and is packaged in a transistor like TO92 format. The sensor can operate at either 5 volts or 3 volts, but interestingly will produce greater accuracy when operated at 3 volts. Unfortunately because it is an analogue sensor it is difficult to use on a Raspberry Pi.


As the analogue input on the Arduino produces 1024 ‘steps’, so if the maximum voltage is 3.3 volts then each step is

 Voltage at pin in milliVolts = (reading from ADC) * (3300/1024) 

This formula converts the number 0-1023 from the ADC into 0-3300mV (= 3.3V)

Then, to convert millivolts into temperature, use this formula:

Centigrade temperature = [(analog voltage in mV) – 500] / 10

This formula allows for temperatures below zero.

The ADC in the Arduino uses an internal reference voltage of 5 volts, but this can be changed by using


Then connecting the new reference voltage to the AREF pin. In this case we connect the Arduino’s 3.3 volt output to AREF and the connections are shown in the diagram at the top of this page.

Upload the following code and switch to the Serial Monitor to see the temperature sensor in action.

TMP36 temperature sensor test sketch
The TMP36 has three pins, with the flat on top and looking at the pins
Connect leftpin to Arduino 3.3v, and connect Arduino AREF to 3.3volts
Connect centre pin to Arduino pin 15 (A1)
Connect right pin to Arduino Gnd

#define aref_voltage 3.3 // Connect 3.3V to ARef

//TMP36 Pin Variables
int tempPin = 1; //the analog pin the TMP36's Vout (sense) pin is connected to

//the resolution is 10 mV / degree centigrade with a
//500 mV offset to allow for negative temperatures
int tempReading; // the analog reading from the sensor

void setup(void)
analogReference(EXTERNAL); // If you want to set the aref to something other than 5v

void loop(void) {

tempReading = analogRead(tempPin);
Serial.print("Temp reading = ");
Serial.print(tempReading); // the raw analog reading

// converting that reading to voltage, which is based off the reference voltage
float voltage = tempReading * aref_voltage;
voltage /= 1024.0;

// print out the voltage
Serial.print(" - ");
Serial.print(voltage); Serial.println(" volts");

// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree with 500 mV offset
//to degrees ((volatge - 500mV) times 100)
Serial.print(temperatureC); Serial.println(" degrees C");

// now convert to Fahrenheight
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF); Serial.println(" degrees F");

Active Buzzer Alarm Type YL-44


Buzzers come in two varieties, Active and Passive. An active buzzer just outputs a single tone when you connect it to Vcc and ground.   A passive buzzer is similar to a loudspeaker and needs a signal to make it work.

The YL-44 is a small buzzer module which operates around the audible 2 kHz frequency range. It is an active buzzer, which means that it produces sound by itself, without needing an external frequency generator. Taking the I/O pin LOW will turn the buzzer ON and taking this pin HIGH will turn the buzzer OFF (as will leaving this pin OPEN). This device could be controlled by PWM.


 This code will produce a two tone output

/*Example Code for YL-44 Active buzzer
Connect Vcc to 5 volts
Connect Gnd to Gnd
Connect I/O to pin 3

int buzzer = 3 ;// connect the I/O pin on the buzzer to this

void setup ()
pinMode (buzzer, OUTPUT) ;
void loop ()
unsigned char i, j ;// define variables
while (1)
for (i = 0; i <80; i++)
digitalWrite (buzzer, LOW) ; // Turn buzzer ON
delay (1) ;// Delay 1ms
digitalWrite (buzzer, HIGH) ;// turn buzzer OFF
delay (1) ;// delay ms
for (i = 0; i <100; i++) // new frequency
digitalWrite (buzzer, LOW) ;// turn buzzer ON
delay (2) ;// delay 2ms
digitalWrite (buzzer, HIGH) ;// turn buzzer OFF
delay (2) ;// delay 2ms

JYMCU Bluetooth Units, HC-05 and HC-06 Part 1

Part 2 shows setting up the Master HC05 and can be seen here.

It is important to note that although the HC05 and HC06 can be powered from 5 volts the control pins and T/Rx  pins must be 3 volts. Use a logic level converter, an example is shown later. I power the device from the 3 volt output on the logic level converter.

A Piconet is a collection of devices connected via Bluetooth technology in an ad hoc fashion. A piconet starts with two connected devices, such as a portable PC and a mobile phone. All Bluetooth devices are peer units and have identical implementations. However, when establishing a piconet, one unit will act as a master for synchonization purposes, and the other unit will be slave for the duration of the piconet connection.
The JYMCU Bluetooth units come in two varieties, one set permanently as a Slave, the HC-06  and the other that can either be a Slave or a Master, the HC-05. The two units look identical, but the Master unit is usually more expensive. Normally if you are using Bluetooth to connect to an Android device, or PC, then a Slave unit is all you need. If you want to communicate between two Arduino’s then one Adruino can use a Slave unit, but the second Aduino will have to use a Master unit. Up to eight Bluetooth units can exist in a Piconet.

The JYMC can be powered from 5volts but a Logic Level Converter must be used on the Tx/Rx pins. The Bluetooth acts as a Serial Communication device and to ensure the Arduino’s Tx and Rx (pins 0 and 1) are free to upload Sketches. I use SoftwareSerial. To test Bluetooth out I have installed BlueTerm on my Android Phone, the phone then acts as a Master unit and the Arduino acts as a slave. Data can be entered into the Serial Monitor on the Arduino and it will appear on the mobile phone, and the mobile phone or android device can switch the onboard LED on the Arduino ON or OFF. 

Upload the following sketch to an Arduino and connect Gnd and Vcc on the JYMC to Gnd and 5volts on the Arduino. Use a Logic Level Shifter (you can purchase one fromHobbytronics  here for £3. The advantage of this board is that it has a 3.3 volt 250 mA regulator) to connect Tx on the Bluetooth to the SoftwareSerial Rx pin and Rx on the Bluetooth to the SoftwareSerial Tx pin.

Simple Send/Recieve test SoftwareSerial for Bluetooth HC06
Sends data from Serial Monitor and will turn LED on pin 13
ON if the character 1 is sent
OFF if the character 0 is sent
ingnores all other data

Vcc to either 3 volts or 5 volts
Gnd to Arduino Gnd
Tx to Arduino pin 10
Rx to Arduino pin 11

Unless this has been altered the bluetooth unit will be found as 'Linvor'


#include <SoftwareSerial.h>
#define onboardLed 13

SoftwareSerial mySerial(10, 11); // RX, TX

void setup()
Serial.begin(9600); // Open serial communications and wait for port to open:
mySerial.begin(9600); // SoftwareSerial "com port" data rate. JY-MCU v1.05 defaults to 9600.
pinMode(onboardLed, OUTPUT);
digitalWrite(onboardLed, LOW); // turn off onboard LED

void loop()
// Read device output if available.
if (mySerial.available()) {
while(mySerial.available()) { // While there is more to be read, keep reading.
unsigned char charreceived =;
Serial.println(charreceived); // show data from bluetooth
case 49:
digitalWrite(onboardLed, HIGH);
Serial.println("Arduino Led On");
case 48:
digitalWrite(onboardLed, LOW);
Serial.println("Arduino Led Off");

// Read user input if available.
if (Serial.available()){
delay(10); // The DELAY!
}// END loop()

Start the Arduino and switch to the Serial Monitor, start BlueTerm on your Mobile Phone and go to Connect Devices. Click on the Scan for Devices button, you should find the HC05 as Linvor. The red LED on the Bluetooth unit will stop flashing and light continuously. Type something into the input box on the Serial Monitor ( set to 9600 baud and send CR/LF selected) and it should appear on your mobile phone. Now type the number 1 in the BlueTerm terminal screen, the onboard LED will light, type 0 and the LED will go out. All other characters will be ignored.

If nothing seems to be working check that the Tx and Rx pins from the Bluetooth are connected correctly. If you can connect to the HC05 with your mobile phone running BlueTerm then it must be a connection problem. These units run at 9600 baud.


This is the basic circuit, when mounted on a baseboard additional circuitry is added such as a 3 volt regulator and a USB interface.

AT Commands:

When the HC-06 is first supplied with power the LED blinks and AT commands can be sent to it. Once the HC-06 connects to a Master Bluetooth unit it switches to communication mode. The HC 06 has a limited number of AT commands and they are described here. These commands must be sent within 1 second and should not have a CR/LF added.

Default settings of the HC-06:
9600, N, 8, 1
Name linvor
Pincode 1234

Command test:
Sent: AT
Receive: OK

Change baud rate:
Sent: AT+BAUD#
Receive: OKbaud
1: 1200
2: 2400
3: 4800
4: 9600
5: 19200
6: 38400
7: 57600
8: 115200


Sent: AT+BAUD1
Receive: OK1200

Change device name:

Sent: AT+NAMEname
Receive: OKname


Sent: AT+NAMEblue
Receive: OKblue

Change pin code:

Sent: AT+PINxxxx
Receive: OKsetpin


Sent: AT+PIN9999
Receive: OKsetpin

Soil Moisture Meter for the Arduino


Some time ago I needed to measure the water content of soil in various places around the island where I live. I built my own probe from two nichrome wires cast into a small plaster of paris tube about 1cm in diameter. When dry the probe has a very high resistance, but as the soil becomes wet moisture soaks into the sensor and the resistance lowers. The resistance of the probe is therefore dependant on the soil moisture content and the Arduino is nothing more than a resistance meter.

Sensors like the one shown above have become available for around £1 on eBay, although the probe would be more suitable for indoor use in a flower pot than outside in the ground. A simple device to warn you that your plant needs watering, or even one that watered your plant automatically, is easily built.

An amazing example of using the Arduino comes from the Van De Leer vineyard in California. They developed a system called the Vinduino for checking soil water content in a vineyard and you can visit this site here. The sketch switches the current flow in the probe to avoid electrolytic effects corroding the sensor. Vinduino outputs the results to an LCD display and they also show how to build probes using plaster of paris (gypsum).

If you want to try making a simple moisture sensor tha will light a LED when your favourite plant needs watering you can try the following sketch. The sensor shown in the photograph has four connections, Ground, Vcc, analogue and digital. The trip level is set with the potentiometer and output  goes from Low to High when the soil dryness reaches a certain level.

# Example code for the moisture sensor
# Connect the sensor to the A0(Analog 0) pin on the Arduino board
# the approximate moisture levels for the sensor reading
# 0 to 300 dry soil
# 300 to 700 humid soil
# 700 to 950 in water

# define ledPin 13
# define sensorPin 14 // is the same as A0

int trigger = 300; // set the level

void setup(){
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // turn off LED

void loop(){

Serial.print("Moisture Sensor Value:");
Serial.println(analogRead(sensorPin)); // read the value from the sensor
if (analogRead(sensorPin) >= trigger) {
digitalWrite(ledPin, HIGH); // turn on the LED
digitalWrite(ledPin, LOW); // turn off LED

Capacitance Switch controls a Relay on Arduino

The sketch was updated Nov 2015. It can be downloaded from my GitHub page here. For a sketch that will control up to five relays from five switches see my post here.

This sketch uses a small capacitance switch to control a relay. The output from the switch is normally LOW, but when touched the output goes HIGH. The relay used is a single relay made by Ywrobot, it has a single changeover switch output and needs 5 volts to operate. The relay is operated when the control input is HIGH and when this happens the onboard LED turns on and the switch output changes.

The connection diagram shows a 4 capacitance switch, but a single switch would work equally well. The Capacitance switch is powered from the digital pins.

When the sketch is first run the relay is ‘off’, when the switch is touched the relay is switched on and the LED lights, the Arduino LED on pin 13 remains alight as long as the switch is touched. The relay remains switched ON until the capacitance switched is touched for a second time and the cycle can be completed.

The WHILE loop acts as a de-bounce, waiting until the switch is released.

/* Simple sketch to show the operation of a capacitance switch

Chris Rouse Nov 2015

The switch used here is mounted on a small board
with three connections, G (Gnd) V (Vcc) ans Out ( the switch output)
The switch output goes LOW when pressed

Connect Gnd to Arduino pin 5 - supplies 0v
Connect Vcc to Arduino pin 6 - supplies 5 volts
Connect Out to Arduino pin 7

The relay used is a Ywrobot board with 1 relay
The relay is switched on by a HIGH signal

Connect the Relay board to Gnd and 5 volts
and the control pin to Arduino pin 8
boolean latch = false;

# define relay 8 // this is the pin used to switch the relay
# define Gnd 5 // this is the pin used to act as Gnd for the switch
# define Vcc 6 // this is the pin used to act as +5v for the switch
# define capSwitch 7 // this is the capacitance switch output
# define LED 13 // onboard LED

void setup() {
Serial.begin(9600); // used for debugging
pinMode(relay, OUTPUT);
digitalWrite(relay,LOW); // turn OFF the relay
pinMode(capSwitch, INPUT);
pinMode(Vcc, OUTPUT);
digitalWrite(Vcc,HIGH);// this pin will supply 5 volts
pinMode(Gnd, OUTPUT);
digitalWrite(Gnd,LOW); // this pin will supply the GND to the board
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW); // turn LED off

void loop() {

// turn switch ON on first touch
if ((digitalRead(capSwitch) == HIGH) && (latch == false)) {
digitalWrite(relay, HIGH); // relay ON
Serial.println("Relay ON");
digitalWrite(LED, HIGH); // turn LED ON
latch = true;
// wait here until the cap switch is no longer touched
while ((digitalRead(capSwitch) == HIGH)){
digitalWrite(LED, LOW); // turn LED OFF
delay(500); // try to stop debounce

// turn relay OFF on second touch
if ((digitalRead(capSwitch) == HIGH) && (latch == true)) {
digitalWrite(relay, LOW); // relay OFF
Serial.println("Relay OFF");
digitalWrite(LED, HIGH); // turn LED ON
latch = false;
// wait here until the cap switch is no longer touched
while ((digitalRead(capSwitch) == HIGH)){
digitalWrite(LED, LOW); // turn LED OFF
delay(500); // try to stop debounce