How to – Piezo as knock sensor

Hello everyone, in this post we are going to learn how to work with a piezo, that is a device that virbates when it receives electricity: when it moves, it displaces air around it, creating sound waves. However, it is even more interesting to learn how it is possibile to use the same device as a sensor to catch, for example, a knock. For this reason, it is also called knock sensor.


This device exploit the so-called piezoeletctric effect: it means that it is able to create a variable voltage when it is deformed.

Piezoelectric effect

Emitting sound waves

First of all, let me explain how it is possible to emit sound waves. Modifying the voltage on the piezo, we can control sound waves’s frequency.

The circuit is really simply: you have just to link the piezo to the ground and to a digital pin on the Arduino.

This is the sketch: it uses the function  tone() in order to emit sound waves of fixed frequency (in this particular case 1500) for a certain period (in this case 2 seconds). The piezo has been linked to the digital pin 8: if you want to change pin, you also have to modify the value of piezoPin constant with the correct value.

Knock sensor

But, as I said, the piezo can be used also as a sensor. The circuit is a bit different:

Knock sensor scheme

The only difference is the presence of a 1 MegaOhm resistence. Pay attention: now the sensor is connected to an analogic pin (A0).

This is the sketch: every time that the sensor catch a knock, the string “Knock!” is sent through the serial port. In order to avoid to take little movement as knocks, it only considers values greater than the treshold of 100. Just a tip: do not knock directly on the sensor: cover it with an other surface.

Stay tuned!


How to – LCD custom characters

In the last post we have learnt how to interface Arduino with a LCD. Now, we are going to learn how it is possibile to create custom characters, and to show them on the display.

Every cell in which the display is divided is formed by a grid of 5×8 pixels (5 columns and 8 rows). Through code, you can create new custom characters, working directly on these pixels. For example, the “omega” character (Ω) can be divided in pixels like this:

LCD custom character

As you can see, the character is described by an array of 8 byte, that represent rows: every byte contains the information about columns. “1” means pixel is on, “0” pixel is off.

For bigger characters, that do not fit in the 5×8 grid, you can use two or more custom character together.

This is a simple sketch that shows how to create and print custom characters. It uses  createChar() function in LiquidCrystal library.

LCD display custom character

Stay tuned!

How to – LCD Interfacing

Hello everyone, today we are going to learn how to use a liquid crystal display. I use a Hitachi HD44780 LCD, a 16×2 display (16 columns and 2 rows, 32 characters in total), that is cheap and easy to get.

The display has 16 pins: 2 for the power, 2 for the backlight, 1 for contrast adjustement, 3 control lines and 8 for writing data. However, in this post I am going to transfer data in 4bit: so I only use 4 of these last pins. Let’s analyze the pins in detail.

Pin functions

Below, a table with every pin’s name and function:

Pin number Symbol Function
 1  Vss  GND
 2  Vdd  +5V
 3  V0  Contrast adjustement
 4  RS  Register select signal
 5  R/W  Read/write signal
 6  E  Enable signal
 7  D0  Data bus line
 8  D1  Data bus line
 9  D2  Data bus line
10  D3  Data bus line
11  D4  Data bus line
12  D5  Data bus line
13  D6  Data bus line
14  D7  Data bus line
15  BL+  +5V for blacklight
16  BL-  GND for blacklight

Here a more accurated descriptions of certain pins:

  • Pin 3 (V0) is usually connected to a potentiometer, so that the user can apply a voltage variation (from 0 to +5V): in this way, he can adjust the LCD contrast.
  • Pin 4 (RS) is used to control in which lcd memory you want to write. You can select the data register, that hold data which will be printed on the screen, or the instructions register, that is where the LCD look for istructions on what to do.
  • Pin 7 to 14 (D0-D7) are control lines that send or receive data to or from LCD’s registers.

For further information, look up the datasheet.


Because of the number of used pins, there will be a lot of wires, but don’t worry, it’s quite simple. Here’s the scheme:

LCD scheme

This is a test sketch from the tutorial on Arduino official website. It uses LiquidCrystal library.

LCD - Hello world

Stay tuned!