Detecting obstacles using an ultrasonic sensor HC-SR04

Detecting obstacles using an ultrasonic sensor HC-SR04

1 HC-SR04: a cheap ultrasonic sensor

1.1 Description

HC-SR04 front view
Fig.1 – An HC-SR04 seen from front side.

HC-SR04 is a commonly used ultrasonic sensor which is capable to detect obstacles in a range of 2-300cm. The sensor looks like a small PCB having two metal cylinders on the front-side and a small circuit on the back-side (see Fig.1).

1.2 Documentation

In this article we will provide a simple demo to use HC-SR04. This requires a preliminary read of the sensor user manual:
HC-SR04 User Manual

1.3 Pinmap description

The sensor has 4 PIN:

  1. VCC, which must be connected to 5V;
  2. Trig, which is an input PIN to trigger the measurement;
  3. Echo, which is an output PIN which sent out a square wave;
  4. GND, which must be connected to ground.

Note that user manual specifies which GND must be connected first since a floating 5V could permanently damage the sensor.

2 How it works

The two cylinders are actually an ultrasonic receiver (R) and an ultrasonic transmitter (T) which are driven by the circuitry. The working principle is quite simple: when triggered the transmitter shoots some pulses. When sound encounters an obstacle it is echoed back and detected by the receiver. continue reading…

How to drive a HD44780 with I2C backpack with a STM32

How to drive a HD44780 with I2C backpack with a STM32

1 I2C backpack advantages

The HD44780 is a de-facto standard controller for display. We have already use it providing a source code to use a 16×2 LCD with a STM32. In this article we will step over introducing an I2C backpack for that display. Of course we will explain how to edit old code in order to get it work with this new hardware configuration.

Even if it is still popular, this controller was made commercially available in the late eighties. At that time serial communications were not so widespread because their were costly and involved constraint about clock speed. Because of that, the HD44780 comes with a parallel bus and requires up to 16 lines to work (8 for data, 3 for handshake, 2 for supply, 1 for contrast and 2 for backlight). With fastest MCU it is possible to use the 4-bit mode and with a couple of welds and a potentiometer we can reduce to 9 the required lines.

Nowadays, serial communication are largely used and SIPO (Serial-in Parallel-Out) converters are very cheap. Using a SIPO, it is possible to drive a HD44780 with less wires accepting a small extra charge due to additional hardware. Today it is possible to buy our display with the SIPO already mounted with a small extra amount (about 3.90 USD instead of 2.80 USD considering a 16×2 LCD).

The most used solution is based on PCF8574A, an I2C I/O expander from NXP. This solution is available as a backpack ready to be  soldered on our display. The backpack reduce the number of wires to 4: 2 for supply and 2 for the I2C communication. continue reading…