Using a cheap HC-SR04 sonar unit with Arduino

…turns out to be ludicrously easy. Just wire it up – Vcc at 5V, ground, and the trigger and echo pins to a couple of the Arduino’s digital pins:

Arduino and HC-SR04 ultrasonic module

and some nice chap has written an OO library for it. The code to read it turns out to be just this:

#include <Ultrasonic.h>
Ultrasonic *s;
#define TRIG 10
#define ECHO 11
void setup(){
 Serial.begin(9600);
 s = new Ultrasonic(TRIG,ECHO);
}
void loop(){
 long timing = s->timing();
 float distance = s->convert(timing,Ultrasonic::CM);
 Serial.println(distance);
 delay(1000);
}

The noisy coffee jar

So, I wanted a tiny battery-powered audio amp for listening to a bit of music in the bath, garden or whatever. Enter the not terribly good audio amplifier chip, the LM386! Also enter a wooden coffee jar, some veroboard, some hot glue and some components. Here’s the circuit:LM386 audio amp circuit

  • Pins 4 and 6 are the ground and supply pins.
  • Connecting pins 1 and 8 with a 10uF cap turns on some Internal Magic in the chip, raising the gain from 20 to 200.
  • Pin 7 is connected to ground via a bypass capacitor – necessary at higher gains, it sounded bloody awful without it.
  • Pins 2 and 3 are the input; we get rid of any nasty DC with the 100nF cap, and then the pot acts as a volume control. The 1nF cap filters out excessively high frequencies (if I recall correctly.)
  • Pin 5 is the output, which also has some filtering – the big 220uF to filter out DC, and the resistor-capacitor to ground to filter out high frequency noise.
  • The resistor and LED are unlabelled and optional – I’ll assume you know how to work out the appropriate resistor for any LEDs you have lying around.
  • The power supply is a 9V battery – if you want to run this from a wall wart, add a hefty capacitor (100uF or greater) in series with the power supply to smooth out any AC ripple.
I’m using a cheap 4W 8 ohm speaker; you can get about 750mW out of this circuit so a 4W speaker is about right. And here’s what it looks like, installed in the jar:
The noisy coffee jar
And the insides – the circuit’s on a bit of Veroboard, and everything’s stuck in place with hot glue:
The inside of the jar
And in action:

Twitter Updates for 2012-05-30

  • @warrenellis comment systems remind me of the guys on the fringe of the Sermon on the Mount, in Life of Brian… #

Powered by Twitter Tools

Twitter Updates for 2012-05-08

Powered by Twitter Tools

Copyright © Found
Jim Finnis' personal blog

Built on Notes Blog Core
Powered by WordPress