Test LEDs with a Coin Cell Battery

The battery (CR2450) in my garage door sensor was getting low, so I replaced it. I’ll keep the old one in my electronics kit for LED testing, as shown in this video. Touch the longer leg (anode) of the LED to + and the shorter leg (cathode) to . Usually + is the top of the battery where the words are. Don’t worry, you won’t hurt the LED if you connect it the wrong way.

Infrared Hacking

Remember last week’s post about tearing apart a component switch to repurpose parts? I spent some time fooling around with IR after that. I thought it would be neat to recreate the basic functionality of switching between 3 devices. For my proof of concept the devices were simple the 3 status LEDs, but you can imagine the possibilities of turning on different devices or triggering processes run on a computer.

The new microcontroller I got is one I posted about a few months ago, called Puck.js. It has an IR transmitter built-in, so I wanted to use it to mimic a remote. Puck.js is pretty slick. It’s really neat being able to program a device in Javascript through a web IDE over Bluetooth Low Energy. No wires at all!

Here’s a video of my hacking results.

Time for the geeky stuff…

First I tried recording IR commands from the remote using the method shown in the Infrared Record and Playback with Puck.js tutorial. I ran into two problems.

  1. Propping a component into the GPIO pins like the author did doesn’t work for shit. You can’t get a solid electrical connection, especially if you bump it at all.
  2. I kept getting Out of Memory errors on the device because the array would get really large really soon.

My next thought was to wire up to one of my other microcontroller that run Arduino, but the popular IR Library (IRLib2) doesn’t support the chips used in any of the boards I have. So over to a Raspberry Pi Zero. Pretty much every search result mentioned using Linux Infrared Remote Control (LIRC). May of the setup instructions I found were incomplete, but I was able to get things running by taking pieces from these two sites:

I’ll detail the steps that worked for me. Before I go down the software route though, I wanted to make sure the IR sensor worked. The only markings on the component are “71M4” and I have been unable to find a datasheet anywhere to match. Luckily these IR receivers are pretty standard and I had a pretty good idea of the pins from looking at how it was connected in the old device. img_8940I got the idea of hooking up a simple LED test circuit on the data pin from an Adafruit learn guide. Pin 1 is data, going into a GPIO pin on the Raspberry Pi (26 in my case), pin 2 is ground, and pin 3 is power (VCC). You may want to use the 3.3V pin on the Pi to provide your power instead of 5V just to be safe, or consult the datasheet for the IR sensor you’re using. Connect the anode of the LED to power and the cathode to pin 1 of the sensor using a 220 Ω (I used 200) resistor. When you press buttons on an IR remote, the sensor will send data through pin 1 and the LED will light up. Here’s a Fritzing wiring diagram for this test as well.

ir-sensor-test-fritzing.png

My test was successful! Now I was able to move on with some confidence knowing the part worked.

Install LIRC:

sudo apt-get update
sudo apt-get install lirc

Edit the /etc/modules file:

sudo nano /etc/modules

Add to the end:

lirc_dev
lirc_rpi gpio_in_pin=26

Change the pin if you’re using something other than 26. If you’re also going to do IR transmitting, you can add a space and gpio_in_pin=22 on that last line.

Press Ctrl + X, hit Y to say you want to save, and then Enter.

Edit the /etc/lirc/hardware.conf file:

sudo nano /etc/lirc/hardware.conf

Look for the DRIVER, DEVICE, and MODULES settings. Set them to match:

DRIVER="default"
DEVICE="/dev/lirc0"
MODULES="lirc_rpi"

Press Ctrl + X, hit Y to say you want to save, and then Enter.

Edit your /boot/config.txt file:

sudo nano /boot/config.txt

Look for this line:

# Uncomment this to enable the lirc-rpi module

If you see it, remove the # from the next line and edit it to look like this (if your file doesn’t have it, add this to the end of the file):

dtoverlay=lirc-rpi,gpio_in_pin=26

If you’re going to do transmitting, also add this to the same line:

,gpio_out_pin=22

Change both pins to match whatever you’re using. Press Ctrl + X, hit Y to say you want to save, and then Enter.

Reboot your Pi:

sudo reboot

Now it’s time to use LIRC to record the codes sent by whatever remote you’re using. First you’ll want to see names you want to give your buttons. Run:

irrecord --list-namespace

Scroll through the list and make notes on all of the codes you want to use for your buttons. You’ll need the codes in a bit. Here was my list:

KEY_POWER
KEY_1
KEY_2
KEY_3

Stop LIRC:

sudo /etc/init.d/lirc stop

Use irrecord to create a configuration file for your remote. Follow the instructions carefully that come up on your screen. This took me several minutes for my remote with only 4 buttons.

Note: When it says Please enter the name for the next button (press to finish recording) is when you’ll need those codes above.

irrecord -d /dev/lirc0 ~/lircd.conf

When finished you’ll have a new file in your home directory. Take a look at it:

cat ~/lircd.conf

Mine looked like:

begin remote

  name  /home/pi/lircd.conf
  bits           16
  flags SPACE_ENC|CONST_LENGTH
  eps            30
  aeps          100

  header       9004  4474
  one           580  1666
  zero          580   542
  ptrail        578
  repeat       9006  2229
  pre_data_bits   16
  pre_data       0x61D6
  gap          107888
  toggle_bit_mask 0x0

      begin codes
          KEY_POWER                0x7887
          KEY_1                    0x40BF
          KEY_2                    0x609F
          KEY_3                    0x10EF
      end codes

end remote

Make a backup of the default LIRC configuration file:

sudo mv /etc/lirc/lircd.conf /etc/lirc/lircd_original.conf

Move your new configuration file over:

sudo cp ~/lircd.conf /etc/lirc/lircd.conf

Restart LIRC:

sudo /etc/init.d/lirc start

Install the Python LIRC library:

sudo apt-get install python-pylirc

Create a pylirc.conf file:

nano pylic.conf

You need to set up each button similar to what mine looks like:

begin
  remote = *
  button = KEY_POWER
  prog = pylirc
  config = KEY_POWER
end

begin
  remote = *
  button = KEY_1
  prog = pylirc
  config = KEY_1
end

begin
  remote = *
  button = KEY_2
  prog = pylirc
  config = KEY_2
end

begin
  remote = *
  button = KEY_3
  prog = pylirc
  config = KEY_3
end

Do a simple copy/paste and change the button and config for each entry.

Press Ctrl + X, hit Y to say you want to save, and then Enter.

Create a basic Python test program:

nano pylirc-test.py

Paste in:

#!/usr/bin/python

import pylirc

pylirc.init( 'pylirc', './pylirc.conf', 0 )

while ( True ) :
	s = pylirc.nextcode( 1 )
	command = None
	if ( s ) :
		for ( code ) in s :
			print( code["config"] )

Press Ctrl + X, hit Y to say you want to save, and then Enter.

Run the program:

python pylirc-test.py

Press buttons on your remote and if everything is working you’ll see the special name codes being output for each button you press.

pylirc-test-output.png

Hit Ctrl + C to stop the program.

I already had all of the logic written for the buttons to work and switch LEDs, so it was easy to add in a little more code to take action when the appropriate IR codes were received.

Once I found the correct information, setup on the Pi was quite easy. A lot of steps, but easy stuff. Making the Puck.js duplicate my Infrared remote’s codes was a bit of a challenge. From the Puck.js Infrared tutorial I linked at the beginning I knew I needed to have an array of pulse lengths, but I didn’t have anything like that from the LIRC configuration. All I had was some hex values for each code:

  • KEY_POWER: 0x7887
  • KEY_1: 0x40BF
  • KEY_2: 0x609F
  • KEY_3: 0x10EF

Combined with another hex code for pre_data (0x61D6) from the lircd.conf file, I had more complete codes:

  • KEY_POWER: 0x61d67887
  • KEY_1: 0x61d640bf
  • KEY_2: 0x61d6609f
  • KEY_3: 0x61d610ef

I searched all over for tools to reverse these into pulses or “Pronto Hex” values, which I also found could be used with Puck.js by decoding them. I couldn’t find anything. At some point I came across the Infrared remote control signals repository on GitHub.

Infrared remote control signals from the LIRC remote configurations project, converted to Pronto Hex and Protocol, Device, Subdevice, and Function using lirc2xml

BINGO! It had the Pronto Hex codes. I cloned the repo and started searching for my hex values. I found power, 1, and 2 matched up with codes used by something called a gigabyte TV. I plugged the codes into a program and they worked! I was only missed the code for button 3.

Then I spend way too much time still searching around. I knew enough about how IR worked and had 3 codes. I finally realized I should be able to figure out what changes to make in order to get my 4th and final code. I converted the hex values to binary:

  • KEY_POWER: 0x7887 = 111100010000111
  • KEY_1: 0x40BF = 100000010111111
  • KEY_2: 0x609F = 110000010011111
  • KEY_3: 0x10EF = 001000011101111

Then I started looking at the end of each array of Pronto Hex codes, because every code uses the same pre_data. I quickly determined an ON bit (1) was 003e, OFF (0) was 0013, and they were separated by 0017. I made the necessary adjustments and had all 4 buttons working with IR!

This IR journey turned out to be quite an adventure. I learned a lot, which was the point. My infrared-3-input-selector project on GitHub has the Python program used in the demo video, my pylirc config file, the simple pylinc test program, the Puck.js code, and even an Arduino sketch with the button and LED logic I created initially before realizing I needed to switch to the Raspberry Pi.

Repurpose

component-video-switchAfter watching recent Adafruit videos (12, & 3) about IR and getting a neat new microcontroller which has a built-in IR transmitter, it looked fun to hack around with. I don’t have an IR receiver though. Then I remembered this old component video switch was in a storage closet. I tore it apart and easily got out the IR receiver. While I was destroying the device I figured I might as well take a bunch of parts that may be useful. If nothing else it was good practice desoldering.

The big PCB with a lot going on is already gone with the trash. The two small PCBs with buttons and LEDs (a triple set and a single) are cool and will be fun to work with since they’re already wired up. Would be neat to use these in an actual project some day.

img_8931The other components I kept are:

  • IR 4 button remote
  • L7805 voltage regulator
  • PIC16C505 CMOS microcontroller
  • HD74HC126P quad buss buffer

  • 2x CD4052BE analog multiplexer/demultiplexer
  • HA17358A dual operational amplifier
  • 78L09L voltage regulator
  • A1515S PNP transistor
  • 71M4 IR receiver sensor

Of course, I can’t find a datasheet for the one part I want to use. IR is pretty standard, so I’m hoping they didn’t go rogue when developing this device. I’ll post more once I get a chance to experiment.

Several months ago I would have had no idea what any of this stuff was, let alone how it worked. I still don’t know what several of those ICs do, but at least I’m able to look at the traces on the PCBs and follow connections to get a general idea of how everything works.

Never stop learning!

Combining 74HC74 & 555 Integrated Circuits

After working with some basic 74HC74 and 555 circuits, it was time to get fancy. I replaced one of the button triggers from my 74HC74 circuit with a 555 timer delay.

74HC74-555-button-wiring.png

Then I replaced the other button with a 555 timer delay as well.

555-74HC74-555-wiring.png

What do you think happens if I swap out the 22 μF capacitors for 4.7 μF? Remember the capacitor charge time formula from the 555 post? Multiply the capacitance (farads) by the resistance (ohms) to get the time. I’m still using the same 100 K ohm resistors.

t = RC

100000 * 0.0000047 = 0.47

So the delay decreases from 2.2 seconds to 0.47.

There is really no point in the 74HC74 here. You can connect two 555s to each other for a similar result. The video shows a double 555 circuit with 3 different timings, where I swap the capacitors from 22 μF to 4.7 μF and then 1 μF (delay of 0.1 second).

double-555-wiring.png

Are there any other circuits I should try with the 74HC74 and/or 555?

Using a 555 Integrated Circuit

555-pins

555 Pins

I posted about the 74HC74 flip-flop on Saturday. For the same project I’m going to use that IC for, I’ll probably use a 555 timer. It’s often referred to as one of the most useful ICs you can get. I’ve never used the 555 either, so I wired up some simple demos using it. In order to show two common timing uses, I’ve created similar circuits each triggered by the same power source and button.

The circuit on the left shows a delay off timer and the one on the right shows a delay on timer. Notice when power is connected (or the button is used as a reset) that the red LED turns on right away and turns off after a few seconds. Just the opposite, the white LED is off when the timer is reset and turns on after a few seconds.

The length of the delays is determined by the capacitor and resistor used with the 555. I’m using an Adafruit Feather to provide 3.3 volts to the circuits with a 22 μF capacitor and 100 K ohm resistor. Using the capacitor charge time formula to multiply the capacitance (farads) by the resistance (ohms), it’s easy to get the time.

t = RC

t = 100000 * 0.000022

Comes out to 2.2 seconds. To change the time delay all you have to do is use a different capacitor and/or resistor.

Here is a simplified wiring setup because it’s hard to see how everything is connected in the video.

555-wiring.png

Also check out Combining 74HC74 & 555 Integrated Circuits.

Using a 74HC74 Integrated Circuit

74HC74-pins

74HC74 Pins

I received some advice to use a 74HC74 flip-flop for a project idea I’ve had. I’ve never used an integrated circuit so I thought a good first step was to put together a very simple demo I could hack around with it. This IC is big enough it actually provides two flip-flops, one on each side as you can see from the pin diagram on the right. Both sides work the same, but are completely independent other than sharing power and ground. I’m only using the first side for this example.

 

As I press the buttons connected to CLR and PRE, you can see the outputs (Q and not Q) alternate. I’ve set the data (D) and clock (CLK) pins to ground. The truth table for the 74HC74 comes in handy to understand what’s going on.

74HC74-truth-table.png

Here is a simplified wiring setup because it’s hard to see how everything is connected in the video.

74HC74-wiring.png

Also check out Combining 74HC74 & 555 Integrated Circuits.

Electronics by Number

I’ve been trying to think of a good way to organize my 100+ resistors. After getting a cheap 1,000 piece ceramic capacitor kit, finding a better way to organize things became a priority. At the local Jo-Ann Fabric and Craft I found these bead storage systems, which contain individual cylinders with screw tops. They work great and there were enough spares that I’m able to store diodes and transistors as well.

Are you wondering why the order of the small canisters isn’t entirely sequential? When there are 3 digits in the marking on a small capacitor, the last digit represents how many zeros to add. For example 104 would be 10 followed by 4 zeros, signifying a capacitance of 100,000 picofarads (0.1 microfarad). Therefore 104 is larger than 683 (68,000).

HC-SR04 as a Motion Sensor

The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. From 2cm to 400 cm or 1” to 13 feet. Its operation is not affected by sunlight or black material like Sharp rangefinders are (although acoustically soft materials like cloth can be difficult to detect). It comes complete with ultrasonic transmitter and receiver module.
Complete Guide for Ultrasonic Sensor HC-SR04

You can get the HC-SR04 from Amazon or various electronics shops for $3-5 or even under $2 if you buy packs of them. I got 2 of them in a parts kit I bought on Amazon and used one for Blog in a Box Paparazzi.

I was using the sensor to sort of detect motion, or more specifically when someone walked into a room. My prototype was set on a desk at about chest height about 1-2 feet after the doorway. While working on the project I ran into several challenges:

  • Accuracy of readings.
  • Other activity on the Raspberry Pi.
  • Sampling over multiple readings.
  • Rogue readings vs actual motion.
  • Timing between readings.
  • Bailing if an echo takes too long.

I wrote my code in Python and heavily based it on ModMyPi’s blog post HC-SR04 Ultrasonic Range Sensor on the Raspberry Pi. Here are the important pieces…

def read_ultrasonic() :
	# Make sure the trigger pin is clean
	GPIO.output( ULTRASONIC_TRIG_PIN, GPIO.LOW )
	# Recommended resample time is 50ms
	time.sleep( 0.05 )
	# The trigger pin needs to be HIGH for at least 10ms
	GPIO.output( ULTRASONIC_TRIG_PIN, GPIO.HIGH )
	time.sleep( 0.02 )
	GPIO.output( ULTRASONIC_TRIG_PIN, GPIO.LOW )

	# Read the sensor
	while ( True ) :
		start = time.clock()
		if ( GPIO.input( ULTRASONIC_ECHO_PIN ) == GPIO.HIGH ) :
			break
	while ( True ) :
		diff = time.clock() - start
		if ( GPIO.input( ULTRASONIC_ECHO_PIN ) == GPIO.LOW ) :
			break
		if ( diff > 0.02 ) :
			return -1

	return int( round( diff * 17150 ) )

def is_ultrasonic_triggered() :
	global prev_ultrasonic

	# Take 6 readings
	for i in range( 6 ):
		ultrasonic = read_ultrasonic()
		#Shift readings
		prev_ultrasonic = ( prev_ultrasonic[1], prev_ultrasonic[2], prev_ultrasonic[3], prev_ultrasonic[4], prev_ultrasonic[5], ultrasonic )

		if ( is_light_enough()
				and prev_ultrasonic[0] != -1
				and prev_ultrasonic[3] < ULTRASONIC_DIST and prev_ultrasonic[4] < ULTRASONIC_DIST and prev_ultrasonic[5]  ULTRASONIC_DIST and prev_ultrasonic[1] > ULTRASONIC_DIST and prev_ultrasonic[2] > ULTRASONIC_DIST ) :
			#print 'Ultrasonic: {0}'.format( prev_ultrasonic )
			return True

	return False

while ( True ) :
	if ( is_ultrasonic_triggered() ) :
		take_picture()

It worked alright, but triggered a little too often. About a week later I came across the Python package gpiozero, which makes it easy to work with a bunch of common Raspberry Pi GPIO components. I wrote an alternate version of BIAB Paparazzi using this package, which worked a bit better. It was so much simpler with gpiozero because it has built-in support for the HC-SR04. All I had to do was initialize the sensor and tell it what code to run when something in range was detected.

ultrasonic = DistanceSensor(
	echo = ULTRASONIC_ECHO_PIN,
	trigger = ULTRASONIC_TRIG_PIN,
	max_distance = ULTRASONIC_MAX,
	threshold_distance = ULTRASONIC_DIST )

ultrasonic.when_in_range = take_picture

The neat thing about the gpiozero package is when you initialize a sensor it automatically starts taking readings, keeps the values in a queue, and does comparisons against an average. My code attempted to do something along those lines, but was much more rudimentary. As nice as this version sounds, it still triggered too often. You can find the complete code for both versions in the BIAB Paparazzi repo on GitHub.

I think I was pushing the limits of what the HC-SR04 is meant for. Most of the examples I’ve seen are people using these to detect approaching walls on a robot. The biggest issue I ran into was the inaccurate readings. For example I’d be getting readings of about 160cm and then out of nowhere it would return a distance of 80-90 cm, even several in a row at times.

At the end of the day there are reasons it’s such a cheap sensor. 😉 For a couple of dollars, what do you expect? I’m curious to try my code on a more powerful Raspberry Pi 3 and see if it works any better. Was the less powerful Pi Zero causing problems?