Slot Machine

I remember when I was a 17-year-old, recent high school graduate, and was traveling with my grandparents from California back to their home in Michigan.  Of course we stopped in Las Vegas and walked the Strip just to see what there was to see.  Almost all of the casinos were open at the front so I wandered into one with my grandparents.  I saw a nickel slot machine and just had to stick a coin in.  Surprise, surprise, I hit the jackpot!  Jackpots were paid out as two rolls of 50 nickels each so a casino employee came over when the lights and sound went off.  He looked at me, handed the rolls to my grandmother and quietly told her that anyone under 21 really wasn’t supposed to be in there.

I like technology but I’m not a tech junkie by any means and I also like a lot of “old school” stuff like antiques, cars, etc.  For instance, if I ever decided to buy a pinball machine I’d want one from the 60’s or 70’s before they got too glitzy.  I’m also not a gambler but I remember how cool that old time slot machine was.  Just for fun I decided to check some out on ebay and about fainted when I saw the price tags.  I could probably afford it but I’m too cheap and there’s no place in our home for it anyway.  Still, I thought that the young grandkids might enjoy a toy like that when they come to visit so I decided to see if I could build a small version.


Old time slot machines had three windows and mechanical wheels with a variety of images that would spin behind the windows when a coin was inserted and the side handle was pulled.  The spinning wheels would come to a stop one at a time and some sort of payout would occur for various matching images.  They also tended to have flashing lights and noise if a jackpot occurred.  I wasn’t trying to exactly recreate an actual slot machine but at least it had to have some of the basics.  I needed a solenoid to activate the trap door for the coins when a jackpot occurred and some LED’s for flashing lights.  I also wanted some sort of sound so I found a sound recorder module in my junk box and recorded the ever popular phrase “Winner, winner, chicken dinner”.  It plays through a small speaker.

The display is one of the large digit surplus 1601 LCD’s that I have lying around.  I used square bracket characters to simulate three windows and finally decided to just use numbers instead of characters for the “tumbling” wheels.  I added a small buzzer to make a clicking sound while the digit “wheels” were turning.  I hemmed and hawed about whether or not to trigger the “spin” when a coin was inserted or to build a separate handle.  The coin slot I bought came with a coin reject button so I decided to use that to start the spin.  I mounted a micro switch so that it is activated when the coin reject button is almost fully pressed.  A piece of scrap PVC pipe was placed between the coin slot and the trap door to hold the inserted coins.

A big part of the fun was doing a little woodwork (one of my other hobbies) to build the cabinet.  I didn’t have anything that was quite right in my scrap pile so I headed to the local lumber store to look around.  One of the types they carry is poplar which, to me, sounded pretty bland.  But when I started sorting through boards I came across one that I just had to have because of its variety of color bands.  It was only later that I found out that it is an example of what is termed “Rainbow Poplar”.  Unfortunately, my photography doesn’t really do it justice.




Slot Machine

The schematic is shown above.  Most of the components have already been described in the previous section and are fairly obvious.  I placed four different color LED’s on the front, two on each side, and they flash back and forth when a jackpot is hit.  The schematic just shows those as two LED’s on PIC pins 11 and 12.  The sound module says it will run on 5 volts but the specs show it being happier with less than that.  Instead of a regulator I just put a couple of salvaged 1 amp diodes in series to drop the +5 volts to the module.  The inputs to the module prefer 3.3 volt levels so a resistor divider was added to the trigger input.

The solenoid runs on 12 volts and originally I had planned to just use a 7805 regulator to provide the +5 volts for the logic.  Because of the current draw of the large LCD, there was a lot of power being dissipated so I grabbed a simple DC-DC buck regulator board from my parts supply to do the job.  The small solenoid I used is not very powerful and won’t retract if too many coins are pressing down on the trap door.  That shouldn’t be a problem because I made the odds 8:1 to keep the kids interest up.  In any case, I decided to use a generic N-channel FET to activate the solenoid in order to reduce the voltage drop.


The main routine just loops continuously until the button on the coin slot is pressed.  There is no requirement to insert a coin first to activate the spin cycle but I’m hoping that the kids don’t figure that out.  While the main routine is looping, it is incrementing the variable “Random”.  It will simply overflow back to zero the cycle after it hits 255.  When the “Spin” routine is called it just goes through a list of checks on the value in “Random” to determine if a winner has occurred.  As stated earlier, the odds are set at 8:1 but it can be easily changed by modifying the winning values in “Spin”.  If a matching value is not found, then the logic just shifts bits in “Random” and displays each three bits as a number.  A test is done to ensure that an accidental match of digits doesn’t occur.

The power up display shows a question mark in each of the three windows.  While the “wheels” are spinning, each window will alternate between a blank and a question mark and then finally settle on a number one at a time.  That logic is contained in the routine “Send_Digs”.  Instead of having to constantly rewrite the entire display, the routine “Send_Dig” writes to a specific LCD location.  Also, while the “wheels” are spinning, the routine “Clickit” is called to simulate the mechanical sound of the wheels.  This is accomplished by sending 2ms on / 100ms off sequences to the piezio buzzer.

When a jackpot occurs the LED’s alternate flashing from right to left, the sound module is activated, and the coin door is released.  There are no partial payouts, just all or nothing.  After the coins are collected the coin door must be manually pushed back up to latch.  That’s it for this post.  Check out my other electronics projects at:

Slot Machine

Etekcity Wireless Socket Hacks


There are a zillion RF remote controlled outlets available but one of the most popular appears to be those from Etekcity.  I was able to pick up, at regular price, a set of five and two remote controls for less than $30 on Amazon.  I wasn’t sure what I was going to do with them but I figured it was a prime opportunity to do some hacking.

One thing to keep in mind is that these are only controllable with the included remote, and not via the internet.  But we will fix that.  Also, they are normally off when plugged in and go back to that state if power is lost.  I don’t know about you, but I have some applications where I want the outlet to be normally on instead.  We will fix that as well.  Just keep in mind that these hacks require some knowledge of electronics and basic soldering skills.

Making Normally On Outlets

Changing one of these outlets from normally off to normally on would seem to be a simple task because they use a pretty standard relay which should have pins for both states.  As it turns out the relay may or may not have the normally on pin, but it is not accessible on the circuit board.  That complicates our task but it’s probably a smart safety move by the manufacturer.  What that means, then, is that we need to find a way to reverse the on/off logic.



LED Replaced

There are two pieces to reversing the logic.  The first is to change the polarity of the LED.  The solder pads for the LED are shown in the first picture.  Once the LED is removed, we need to make two cuts to the circuit traces as shown in the second picture.  The right hand cut separates the LED solder pad from ground.  We do that so that after the LED is reversed, we can solder that pad to +5 volts.  The left hand cut separates the base of the relay driver transistor from the 4700 ohm resistor.  That will allow the second logic polarity reversal to be installed.  Double check with an ohmmeter to make sure that the cuts are successful.  In the third picture we have reinstalled the LED with the anode now connected to the cut pad and to +5 volts.  The leads were just long enough on my unit so that I could bend it over to the +5 volt output of the 78L05 voltage regulator.

Transistor Added

The fourth picture shows the method used to reverse the logic for driving the relay.  I used a common 2N3904 NPN transistor (an equivalent would be ok) as an inverter.  The emitter is soldered to ground, the base is soldered to the on-board 4700 ohm resistor, and the collector is soldered to the base of the relay driver transistor.  In order to ensure that the relay driver transistor is normally switched on, I had to add a 4700 ohm resistor from its base to +5 volts.  Now, when the logic output is high, it will turn on the new transistor which will turn off the relay driver transistor.

Remote x-wired

If you want to take an extra step you can cross-wire the appropriate buttons in the remote so that the left button will turn on the modified outlet and the right button turn in off.  Basically you need to cut the circuit traces that go to the switch contacts that are closest to the middle of the board and then add jumper wires as shown in the picture

Internet Control

There are two methods possible for controlling the RF outlets from the Internet.  Both require the use of a cheap module like the ESP8266.  One method would be to wire into one of the remote controls and use a microcontroller to simulate the button presses.  The other less messy method is to use a microcontroller to take the place of the remote control.  That is what is described here.  The microcontroller will receive commands via the ESP8266, translate them into the proper RF bit pattern, and then send that bit pattern to an RF transmitter.  It sounds complicated but the only hard part is figuring out what the proper control codes are for your set of RF outlets.

There are many posts online that use an RF receiver and the audio input to a PC to figure out the codes.  I have the luxury of having a decent oscilloscope so it is easy for me to capture them.  I also have an RF sniffer circuit (detailed in one of my other electronics projects on my website) that allows me to capture RF transmissions using a terminal program on my PC.


The frequency for communicating with the RF outlets is 433.92-MHz and the commands are comprised of a long sync bit, 24 data bits, and 1 stop bit.  The data encoding method used is On-Off-Keying (OOK) which means that data bits are differentiated by the on/off times.  There are no requirements in OOK for number of bits or period length.  That’s why there are so many variations out there for different devices.  I have seen that first hand by decoding security sensors and weather sensors.  The waveform looks similar to what is shown in the picture here.


PIC WiFi Interface2

The schematic shown here is almost identical to the one I used in the one of the earlier Wi-Fi projects listed on my website.  The main difference is that the final version doesn’t have the USB interface but does have an interface to an RF transmitter module.  The transmitter module I used is labeled FS1000A and transmits at 433.92-MHz.  I have not tried other models of RF transmitters but most should work as long as they have similar characteristics.  The RF module is run from the +5 volt input and readily accepts the 3.3-volt logic level for the serial data bit stream from the PIC.

Some ESP8266 modules have their own 3.3 volt regulator onboard so the input to it would be 5 volts.  I have included a 3.3 volt regulator in my schematic for the PIC and it can also be used for the ESP module if it doesn’t have its own voltage regulator.  This allows the PIC and the ESP to communicate at the same logic levels without the need for converters.

You could simplify the ESP hardware by using the ESP-01 module and the adapter (shown here).  The adapter takes +5 volts and has an onboard 3.3 volt regulator.  If you go this route I also recommend that you buy the USB interface that is specifically made for the ESP-01.  It will make the setup of the ESP-01 much easier.


The software listing is available below.  It is an extension of the software I wrote for a previous Wi-Fi project.  I chose that because I wanted to have the status response from the PIC displayed as simple graphics instead of text.  I also added code to output the single-pin serial bit stream to the RF transmitter.  Like the earlier version, I used HTML commands to draw circles that represent the status of each of the five remote switches.  Red=off, green=on, and white=unknown.  The important thing to remember is that there is no feedback from the remote switches themselves so the software can only maintain the status of the last command sent for each switch.  That means that every time there is a power up of the controller hardware the switch statuses are all unknown.  That’s it for this post.  Check out my other electronics projects at:

ESP8266 RF Socket Controller

I2C 4-Digit LED Interface

In an earlier project I detailed an I2C (two-wire) interface to the common 1602 LCD.  Recently I found a cheap 4-digit LED module that uses a pseudo I2C interface.  I don’t have a particular need for it but I went ahead and added one to a parts order I made.  If you know a little about I2C you know that it is a serial interface where the controlling circuit provides the clock and controls the direction of the single data line.  Usually you can hang several I2C modules on the same pair of line (e.g.: clock module and display module) because each module has a unique address.  I said this LED module has a “pseudo I2C” interface because it does not have an address.  That means that it needs to be the only device using the data line.

LED I2C Module

TM1637 Module

The module I have is similar to the one shown here.  It is typically listed as part number TM1637.  You can find a variety of information on the web but I had a hard time finding all of the details I needed.  I also looked at some existing code (Python and C) which can often be a dangerous thing to do.  The one critical thing I determined from the code was that the data bits sent to the LED module need to be LSB first.  That differed from the approach for the other I2C projects I have done.  Another idiosyncrasy that I noted is that the addresses for the digits go from left to right (starting with address C0).  That means that data must be written from most significant digit to least significant digit if using the auto increment addressing mode.  In fixed addressing mode you would just pick the appropriate digit and write directly to it.

I2C Communications

I2C communications are accomplished by using just two wires: one for data (SDA) and one for a clock (SCL).  The data line is defined as being bidirectional so there needs to be a “master” device on the bus to control things.  The clock rate is limited by the slowest device on the bus and controlled by the master device.  Basically it sets a data bit and then toggles the clock line.  At the end of each byte transfer the master reverses the direction of the data line and then clocks in the acknowledgement bit from the slave device.

There are three basic commands for the LED module.  One controls the on/off and brightness of the display, one controls the address of the digit to write, and one controls the addressing mode (fixed or auto increment).  Usually an application will just load all four digits each time so the auto increment mode is normally used and the address set to the first (left-most) digit.


LED I2C Interface

The connections to the PIC are pretty simple with only two I/O pins being used.  That leaves four other pins on our little 12F683 for getting sensor data.  This same connection can be used with the 16F688 if we want more I/O pins.  The module already has the required pull up resistors on the SDA and SCL lines.


The software link is listed below.  While it is targeted for the 12F683, it is easily ported to bigger versions of the PIC like the pin compatible 16F688 that I use for a lot of my projects.  Some of the newer PICs also have a hardware I2C interface so most of the related software routines would be greatly reduced or eliminated.

The I2C routines are the same as those I wrote for the LCD project but a lot of the other LCD-specific code was not needed for the LED module.  I added a lookup table that converts single packed ASCII characters to the required LED segments.  I also added a second test routine that shows how you can make non-ASCII displays by turning on the right segments.  The data bit associations for the segments are shown in the header of the software source code.  The eighth bit in the segment bytes is reserved for turning on the colon that resides between digits 2 and 3 on the module.  In my tests it appeared that all four characters had to include a 1 in the eighth bit in order for the colon to light.

I added a line of code to the end of the “i2cwaitack”routine to try and recover if the slave does not acknowledge the data transfer.  If the slave fails to acknowledge a transfer then the software will be reset by enabling the Watch Dog Timer (WDT).  Newer PIC chips have a software reset command that can be used instead.  An alternative for some I2C applications would be to simply make a call to “i2cstop” and then continue where the code left off.  That’s it for this post.  Check out my other electronics projects at


Rootin’, Tootin’, Shootin’ Game

When I lived in Orange County, California two of the biggest employers of college kids were Disneyland and Knott’s Berry Farm.  Because I had electronics training from the military I was able to get a job in the Knott’s shooting gallery instead of having to wear a funny costume.  The rifles used high voltage flash tubes with focusing lenses and the targets used photo cells.  The target counter circuits used germanium transistors set up as flip-flops.  The transistors were becoming harder to find so someone had tried replacing them with silicon ones.  Unfortunately, they found out that the fast switching times of the silicon transistors made them much more susceptible to noise.  That meant that a single hit on the target would ripple through the counters and light all of the lamps at once.  The lesson here is that sometimes slow is good.

Recently I was thinking about those days and decided to see if I could design a simple shooting game for my grandkids.  The game detailed here pits two players against each other to see who can get to five hits first.  I also decided to use a cheap red laser diode as the heart of the gun.  You could use laser pointers if you want but the circuit I include for the gun ensures that you get a single shot instead of a steady on beam.

Light Sensor Modules

Light Sensor Module

At first I was going to just use photo transistors for the sensor circuits but then I discovered the light sensor modules shown above.  I bought a pack of 10 for next to nothing from a China supplier.  The modules do use a photo transistor but they run the sensor voltage into an LM393 comparator so it provides a digital output as well as an analog one.  A potentiometer onboard can be adjusted to set the trip level of the comparator.  It also includes a power on LED and an LED that lights when the comparator switches the digital output.  That makes it easier to adjust the proper level.

Target Hardware


The bulk of the hardware consists of 10 LEDs and 10 resistors.  I used standard 5mm bright white LEDs for indicators 1-4 and a slow flashing LED for the 5th indicator.  The switch is normally open momentary contact and is used to reset the game.  The PIC microcontroller is a standard one that I’ve used in other projects.  As you can see in the pictures, I built the LED modules separately in order to make it easier to locate them in a target.

Target Layout

Target Top

Gun Hardware

Laser Gun

The basic hardware and schematic for the laser gun is shown above.  I built mine into plastic toy airsoft guns.  The barrel tube for the pellets is almost the perfect size for the laser diode modules and I was able to fit a battery holder for two AAA batteries in the opening for the magazine.  There are plenty of cheap laser diode modules out there and basically they differ only in the value of the current limiting resistor mounted onboard.  That resistor determines the voltage rating of the laser module.  I use two AAA batteries so I picked 3 volt lasers.  The switch is a single pole, double throw micro switch.  The capacitor is used to force a single burst of light with every pull of the trigger.  In one position of the switch the capacitor charges up and in the other position it discharges through the laser.


Like all of my PIC projects, the software is written in assembly language.  What makes this project a bit unusual is that the Main routine does nothing because all of the action takes place in the interrupt handler.  The PIC has a feature called interrupt-on-change which, in older PICs, generates interrupts on any positive to negative or negative to positive transition on an I/O pin.  This particular PIC allows the software to set the interrupt source to be either the positive edge, negative edge, or both edges.  The light sensor module will generate both edges on a transition so this feature is quite handy.  In this case, the software waits until the sensor output switches back high (off) before the interrupt is generated.

When a sensor interrupt is received, the software temporarily disables that input and sets a timer.  In effect, the timer acts like a debounce circuit for a switch.  At the 8-MHz clock selected for the PIC and the setup for the timer, the total timeout is about 130ms.  When the timer finishes, it also generates an interrupt.  At that point, the sensor input is re-enabled.  Each sensor input has its own dedicated timer so there is no conflict between players.

Each sensor interrupt will also light one of the LEDs for that player.  Instead of a counter, the software uses a variable that has one bit set.  That bit gets left shifted with each interrupt and is then OR’ed into the output port to light the next LED.  When the last LED is lit, the interrupt handler disables further interrupts and that effectively locks out the other player.  The reset switch is connected to the MCLR input of the PIC and the configuration bits are set to allow that function.  When reset is pressed, the software will reinitialize and clear the LEDs.  That’s it for this post.  Check out my other electronics projects.

Shooting Game SW