8 Seven Segment LED Display with Keys and LEDs using TM1638.

The sketch and library can be downloaded from my GitHub page here

Using the library available from here this board is easy to operate. Connecting the board to an Arduino requires five connections, Vcc to Arduino 5 volts, Gnd to Gnd. STB, CLK and DIO on the display board connect to 7, 9 and 8 on the Arduino.

The library contains a number of examples, but the pins used for Clock, Strobe and Digital Input varies in each example, so I have defined these at the start.

/*

TM1638 8 x Seven Segment Display with Keys and LEDs
Tests some functions
No key pressed displays decimal number
Key 1 pressed displays HEX value
Key 2 pressed displays binary value
Key 3 pressed clears displayed
Key 4 pressed displays a text string

Connections::
Vcc to Arduino 5 volts
Gnd to Arduino Gnd
Strobe, Digital I/O and clock are defined in the sketch
*/

#include <TM1638.h>
const int strobe = 7;
const int clock = 9;
const int data = 8;
int dots = 0x0; // display all decimal points, value in hexadecimal
int leds = 0xFF; // mask to display LEDS
byte keys; // key value
double number; // number to display
// define a module
TM1638 module(data, clock, strobe);

void setup() {
Serial.begin(9600);
module.clearDisplay(); // clear the display
number = 12345678; // decimal number
}

void loop() {
// display a number
  switch(keys){
case 0:
module.setDisplayToDecNumber(number, dots);
break;

case 1:
module.setDisplayToHexNumber(number, dots);
break;

case 2:
module.setDisplayToBinNumber(number, dots);
break;

case 4:
module.clearDisplay(); // clear the display
break;

case 8:
module.setDisplayToString("testing ");
break;

case 16:

break;

case 32:

break;

case 64:

break;

case 128:

break;

default:

break;
}
//

// get a key press
getKeyPressed();
//
}


void getKeyPressed(){
keys = module.getButtons();
Serial.println(keys);
// light the LED
module.setLEDs(keys & leds);
}

Advertisements

Heart Beat Monitor and Arduino

 
I sat down to try and get this tiny device to work and quickly found this website pulse sensor.com that has a complete tutorial on turning this device into a complete heart rate monitor. An Arduino sketch and a Processing sketch provide an output like this:

 

The red heart beats at your heart rate. Download both the Arduino and Processing zip files and unzip them. There are a number of files in each folder, and the file needed is PulseSensorAmpd_Arduino_1dot4 the other files are automatically loaded at the same time. Upload the Arduino sketch and leave the Arduino connected to your PC, open Processing and upload the processing sketch. When you run this sketch you will see the display as shown above. I had an error when I first ran the sketch and needed to change the line

port = new Serial(this, Serial.list()[9], 115200); // make sure Arduino is talking serial at this baud rate

to

port = new Serial(this, Serial.list()[0], 115200); // make sure Arduino is talking serial at this baud rate

The website shows how to make the sensor easy to use, and I found that without shielding the output was variable. I also connected an LED between Arduino pin 5 and Gnd to show pulse rate. The LED attached to pin 13 was supposed to flash, but I did not see this.

This turned out to be a fun project and many thanks for the guys at pulse sensor.com

iBeacon using an HM10 or CC41A Bluetooth 4 Unit

Download the Sketch from my GitHub here.

One of the reasons I wanted an HM10 Bluetooth board was to try making an iBeacon. These have been in the news a lot recently since Apple announced the technology. Google produced their own version called Eddystone.

An iBeacon transmits a unique identification at a regular interval, they can be used to provide information about goods and services as you pass through the relevant department, or even track the route shoppers use as they pass through a Department Store. Using vey low power usage BLE devices can operate for months on a small battery.

An Arduino can be used to set up one of these devices, and once programmed the Arduino can be removed and the Bluetooth unit simply connected to a power source and left to transmit its message.

There is a big difference in the commands available on the HM10 and its clone the Cc41A, and the instructions here will work on both devices, but will not give the most power efficient settings. The transmitting power can be adjusted to give a range in open air of about 110metres, although that probably drops to about 30 metres or so in a building.

The list of commands for the CC41A is shown here. The device is missing a number of commands available on the HM10.


The CC41A is a 3 volt device, connect the Gnd to Arduino Gnd, the Vcc to Arduino 3 volts. The Tx and Rx need to be connected through a Bi Directional Logic Level Converter, like this one  here. I use Software Serial using pin 2 for Rx and pin 3 for Tx, remember the Arduino Rx is connected to the Bluetooth Tx and the Arduino Tx (pin 3) is connected to the Bluetooth Rx.

Upload the Sketch shown below and switch to the Serial Monitor. Make sure NL/CR is selected and the Baud rate is set to 9600. (If the HM10 is used the select No Line Ending). Enter AT and you should see OK displayed, now enter AT+RENEW followed by AT+RESET. The device is now reset to factory settings.

Now enter the following,

AT+MARJ0x1234,  Set iBeacon Major number to 0x1234 (hexadecimal).

AT+MINO0xFA01,  Set iBeacon Minor number to 0xFA01 (hexadecimal)

AT+ADVI5, Set advertising interval to 5 (546.25 milliseconds)

AT+NAMEYOURNAME, identifies this beacon

AT+IBEA1 Enable iBeacon mode

AT+RESET

Install an iBeacon scanner on your phone or other device (I use iBeacon Scanner for my Android phone). Apply power to the Bluetooth device and start the scanner, information from the iBeacon, including the distance from the beacon, will be shown.

I have seen some information that suggests the latest firmware for the HM10 can be used as a scanner, but the CC41A does not seem to be able to to do this.

/************************************************************
BLE CC41A Bluetooth
This is a clone of the HM10 BLE board

In the Serial Monitor ensure that 'Both NL and CR' is selected
Select a Baud Rate of 9600
********************************************************/
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(2, 3); // RX, TX

void setup()
{
// Start the hardware serial port
Serial.begin(9600);
pinMode(13, OUTPUT); // onboard LED
digitalWrite(13, LOW); // switch OFF LED
bluetooth.begin(9600);
}

void loop()
{
bluetooth.listen();
// while there is data coming in, read it
// and send to the hardware serial port:
while (bluetooth.available() > 0) {
char inByte = bluetooth.read();
Serial.write(inByte);
}

// Read user input if available.
if (Serial.available()){
delay(10); // The DELAY!
bluetooth.write(Serial.read());
}
}


Comparing the HM10 and BLE CC41a Bluetooth 


These units will not connect to an HC05/HC06 and although it was visible to my Samsung Galaxy 5 it would not connect.

The HM10 has a crystal fitted, shown as a silver rectangle in the picture above, alongside the bottom four solder connections on the picture right. The CC41A does not have this crystal.

Download the Sketch from my GitHub here.

I saw a pair of HM 10 Bluetooth boards on eBay for less than £6 and purchased them. I wanted to try using Bluetooth 4 and to also use them as iBeacons. They arrived from Hong Kong in less than two weeks and I set about trying to get them to work. I used a simple sketch (shown below) that I use to set up HC05 Bluetooth boards. However, I just could not get them to work, I could see them on my Android phone but the would not connect. The units would not respond to AT being sent, I had set up the Serial Monitor to 9600 Baud and no Carriage Return or Line Feed.

Eventually I turned on NL/CR and sending AT gave the response OK. As I worked my way through the instruction set I found that not all seemed to work. The answer was that I had a BLE CC41A Bluetooth board which is a clone of the HM10. This Bluetooth requires LF/CR to be sent after a command, the default baud rate is 9600, the same as the HM10.

Other differences are that the commands can be in either uppercase or lowercase and the ? is not used, so to report the Baud rate AT+BAUD is used instead of AT+BAUD? The BLE CC41A has a limited command set and not all the HM10 commands are available. Typing in AT+HELP will print out a list of commands.

By default the CC41A is set as a Slave (ROLE=0) and the Baud Rate is 9600. It can be returned to this state at anytime by sending AT+RENEW and once OK has been received send AT+RESET. 

To set the CC41A as a Master, first reset to factory settings as shown above, although this is not strictly necessary at least you know where you are starting from, then send AT+INQ and wait for a reply. The CC41A will report any Slave units it can find, giving each Slave a number. To connect to a Slave send AT+CONNn, where n is the number for the Slave. With just one other Bluetooth available this would be AT+CONN1. The units pair almost immediately with the LEDs on both units changing from flashing to solid. Unfortunately, unlike the HC05, the Cc41A has to be connected each time to the Slave, when power is removed.

The HM10 and CC41A are 3 volt devices, but because the power requirements are low it can be powered from the Arduino 3 volt pin. Use a Bi Directional Logic Level Converter, like this one here. I use Software Serial using pin 2 for Rx and pin 3 for Tx, remember the Arduino Rx is connected to the Bluetooth Tx and the Arduino Tx (pin 3) is connected to the Bluetooth Rx.

Upload the sketch below to both the Master and the Slave and leave the Slave running. Un Rem the lines on the Sketch on the Master Arduino and upload the modified Sketch, once it uploads switch the Serial Monitor on. After 20 seconds or so the Master should connect and the LEDs should remain lit.

/***********************************************************

BLE CC41A Bluetooth Master setup sketch
This is a clone of the HM10 BLE board
In the Serial Monitor ensure that 'Both NL and CR' is selected
Select a Baud Rate of 9600
enter the following commands into the MASTER unit
AT - should return OK
AT+RENEW - restores to factory settings
AT+RESET - software reset
AT+ROLE1 - sets to Master
AT+INQ - searches for nearby Slave units
AT+CONN1 - connects to Slave Unit 1
************************************************************/
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(2, 3); // RX, TX
void setup()
{
// Start the hardware serial port
Serial.begin(9600);
pinMode(13, OUTPUT); // onboard LED
digitalWrite(13, LOW); // switch OFF LED
bluetooth.begin(9600);
// un REM this to set up a Master and connect to a Slave
/*
Serial.println("BLE CC41A Bluetooth");
Serial.println("----------------------------------");
Serial.println("");
Serial.println("Trying to connect to Slave Bluetooth");
delay(1000);
bluetooth.println("AT"); // just a check
delay(2000);
bluetooth.println("AT+ROLE1"); // st up as Master
delay(2000);
bluetooth.println("AT+INQ"); // look for nearby Slave
delay(5000);
bluetooth.println("AT+CONN1"); // connect to it
*/
}
void loop()
{
bluetooth.listen();
// while there is data coming in, read it
// and send to the hardware serial port:
while (bluetooth.available() > 0) {
char inByte = bluetooth.read();
Serial.write(inByte);
}
// Read user input if available.
if (Serial.available()){
delay(10); // The DELAY!
bluetooth.write(Serial.read());
}
}

4×4 Matrix Keyboard with Arduino.

4x4 matrix keyboard

Download the Sketch from my GitHub here.

It’s now possible to purchase a 16 switch keyboard for around £2 from eBay, an example is here. The 16 keys are arranged in a 4 by 4 matrix, four rows by four columns. Rows go from side to side (with the connector pins at the top) and columns go from top to bottom. With the connector at the top key 0 is top left and key 15 is bottom right.

The sketch shown here has a simple debounce routine, reports the key number, then waits for the key to be released. Eight connections are required, four for Rows and four for columns. Pins 2,3,4 and 5 are used for rows and 8,9,10 and 11 are used for columns. On my board, with the connections at the top, the left four pins are Rows and the right four pins are columns.

Once the board is wired to the Arduino upload the sketch below and open the Serial monitor, press a button and its number will be shown. From top left the keys are 0, 1, 2, 3, the next row down, left to right are 4,  5, 6, 7 and so on down to the bottom row which is  12, 13, 14 and 15.

The sketch works as follows, all four Rows are set HIGH, then one Row is set LOW. Each column is read and if a key is pressed the relevant column will be LOW. Using the Row and Column numbers the key number can be calculated. The Rows are reset to HIGH and the next Row is set LOW, the columns are scanned again. If no key is pressed it will take four Row scans to detect this. The key value can be interpreted using a SWITCH loop. Simple switch debounce is achieved by inserting a short delay between keyboard scans.

/****************************************************
4x4 keyboard matrix
By Chris Rouse Oct. 2015
Connect:

Row 1 to Arduino pin 2
Row 2 to Arduino pin 3
Row 3 to Arduino pin 4
Row 4 to Arduino pin 5
Column A to Arduino pin 8
Column B to Arduino pin 9
Column C to Arduino pin 10
Column D to Arduino pin 11

Key identification:
(with connector at the top)
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15

Connector:
Rows     Columns

X X X X  X X X X
1 2 3 4  A B C D
(Rows link keys from left to right,
columns link keys from top to bottom)
****************************************************/
int rowCounter =0; // row counter
int columnCounter =0; // column counter
int foundColumn = 0;
boolean foundCol = false;
int keyValue = 0;
int noKey = 0;
boolean readKey = false;
int debounce = 300; // set this to the lowest value that gives the best result
const int row1 = 2;
const int row2 = 3;
const int row3 = 4;
const int row4 = 5;
const int colA = 8;
const int colB = 9;
const int colC = 10;
const int colD = 11;
const int ledPin = 13; // onboard LED

void setup(){
Serial.begin(9600);
pinMode(row1, OUTPUT);
pinMode(row2, OUTPUT);
pinMode(row3, OUTPUT);
pinMode(row4, OUTPUT);
pinMode(colA, INPUT_PULLUP);
pinMode(colB, INPUT_PULLUP);
pinMode(colC, INPUT_PULLUP);
pinMode(colD, INPUT_PULLUP);
//
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // turn LED off
}

void loop(){
if(noKey == 16){ // no keys were pressed
readKey = true; // keyboard is ready to accept a new keypress
}
noKey = 0;
for(rowCounter=row1; rowCounter<(row4 +1); rowCounter++){
scanRow(); // switch on one row at a time
for(columnCounter = colA; columnCounter <colD +1; columnCounter++){
readColumn(); // read the switch pressed
if (foundCol== true){
keyValue =(rowCounter-row1) +4*(columnCounter - colA);
}
}
}
if(readKey==true && noKey == 15){ // a key has been pressed
Serial.println(keyValue); // used for debug
if (keyValue == 13){
digitalWrite(ledPin, !digitalRead(ledPin)); // toggles LED ON/OFF
}
else{
digitalWrite(ledPin, LOW);
}
/********************************************************
// call to part of the sketch that will use the key number
*/

//*******************************************************
readKey = false; // rest the flag
delay(debounce); // debounce
}
}
void scanRow(){
for(int j =row1; j < (row4 +1); j++){
digitalWrite(j, HIGH);
}
digitalWrite(rowCounter , LOW); // switch on one row
}
void readColumn(){
foundColumn = digitalRead(columnCounter);
if(foundColumn == 0){
foundCol = true;
}
else{
foundCol=false;
noKey=noKey +1; // counter for number of empty columns
}
}

Metal touch sensor, KY036

Download the sketch from my GitHub here.

This seems to be a switch known as an AC hum switch. When you touch the bare wire folded over the end of the transistor mains hum present all around us in a modern house or office is injected into a high gain amplifier. The output of this amplifier is connected to a comparator ic such as an LM358 and the induced AC signal is converted to a square wave as the AC sine wave switches the comparator. The level that switches the circuit is set by the small blue variable resistor and when the output is switched on a LED is lit. If you look carefully at the LED while it is on it appears to be almost flickering rather than steadily lit. This is due to the output being a 50 Hz square wave rather than a steady HIGH voltage.

A typical AC hum detector is shown here.


The output from the DO pin could be simply connected to any Arduino Digital pin and, say, the onboard LED switched on when the signal goes HIGH. However, the 50 Hz square wave output will be constantly switching the LED on and off. The sketch below overcomes this by latching the output ON the first time the signal goes HIGH and stays that way until the sensor is touched again, turning the output OFF.

This shows the squarewave output from DO (AO seems to be an inverted version of DO).

scope

The AC hum switch is an interesting circuit but has a number of disadvantages and if you need a touch switch then it is better to use a Capacitance touch switch, that does not rely on a nearby mains wiring being available. I really can’t see any time I would use this little board, but it was good fun getting it to work.

/*******************************************************
KY036 Metal Touch Sensor
Connect;
AO to Arduinp pin A3
DO to Arduino pin 2
G to Arduino Gnd
Vcc to Arduino 5v
*******************************************************/


// setup touch sensor on pin 2
const int touchPin = 2;
// setup LED on pin 13
const int ledPin = 13;
// store thetime when the last event happened
unsigned long lastEvent = 0;
// store the state of the LED
boolean ledOn = false;
void setup() {
// setup pins
pinMode(touchPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
// read the touch sensor state
int touchState = digitalRead(touchPin);
// only interested in HIGH
if(touchState == HIGH){
// if 50ms have passed since the last HIGH pulse
// touch sensor has been touched or released
if(millis() - lastEvent >50){
// toggle LED and set the output
ledOn = !ledOn;
digitalWrite(ledPin, ledOn ? HIGH : LOW);
}
// remember when the last event happened
lastEvent = millis();
}
}

Remote Bluetooth Thermometer with OLED Display.

Part 2 describing the Receiver is here.

The code for this project can be downloaded from my GitHub page here.

This is a version of an earlier post I made, using HC05 and HC06 Bluetooth modules to make a wireless thermometer. This project uses an OLED Display on the receiver.

This post details the construction and setting up of the transmitter. I have used an Arduino Uno, but a Mega or even Nano could be used. The HC05 can be powered from 3 to 5 volts, but the Tx and Rx lines are 3 volts only. A logic level converter, like this one from Hobbytronics, which has a 250ma 3volt regulator that can be used to power the HC05.

Parts List:
Arduino Uno
HC05 Master/Slave Bluetooth
Bidirectional Logic Level Shifter
TMP36 thermometer

bluetooth transmitter

The connection diagram is shown above, connect a flying lead to the KEY pin unless your breakout board has a small push button fitted. This will be used to switch the HC05 into command mode.
Once the circuit has been built upload the hc05_command_mode.ino sketch. Connect the KEY pin to 3.3 volts (or press the small push button if one is fitted) and apply power, Once the sketch is running remove the connection to the KEY pin (or release the small push button if one is fitted). The HC05 should now be in command mode, running at 38400 baud, check this by opening the Serial Monitor, (set to 9600 and NL/CR) and type in AT and click SEND, OK should be shown.

To set the HC05 up to act as a Master Unit, with it in the command mode enter the following commands into the Serial Monitor, pressing SEND each time.

AT+ORGL this resets the HC05 to factory settings
AT+ROLE =1  this sets the unit to Master
AT+INQM=1,9,48. this allows up to 9 Bluetooth units to be able to connect
AT+CMODE =1  this sets the connection mode to allow any unit to connect
AT+UART=9600,0,0 this sets the baud rate to 9600

Once these commands have been entered remove the power. Now build the receiver.