Coin Toss CircuitBy Jim Stewart
Nuts and Volts Magazine
It's Friday night and you're hungry, so you decide to go pick up some food. Maybe a pizza, or maybe some kung-pao chicken. You love them both, but you must choose one or the other. Do you flip a coin? Yes, but you do it electronically! In this electronics project you will build a coin toss circuit.
|Compnonet Name||Part Description||Maufacturer Part Number|
|R1||100k, 1/4W, 5%||CF1/4W104JRC|
|R2, R3||330Ω, 1/4W, 5%||CF1/4W331JRC|
|IC1||Quad 2-input NAND Schmitt Trigger 14-pin||CD4093|
|IC2||Dual JK Master/Slave Flip Flop 16-pin||CD4027|
|Q1, Q2||Transistor MOSFET N Channel 60V TO-92||2N7000|
|SW||SPST Tactile switch (NO)||BTS-1102B-2|
|GLED||Green LED T-1 3/4||MCDL-5013GD|
|RLED||Red LED T-1 3/4||HLMP-3301|
|PROT||Phenolic Prototype Board||22-508|
How the Coin Toss Circuit WorksFigure 1 shows the circuit schematic. It has three sections: a square-wave oscillator, a JK flip flop and two LEDs (red and green). It uses CMOS digital ICs and MOSFET transistors. The schematic does not show the power and ground connections for the ICs. When powered from a 9V battery both LEDs will light. When you press the switch, one LED shuts off and the other stays lit; but which one? There's a 50-50 chance for either color.
OscillatorThe square-wave oscillator uses one gate in the CD4093 IC. The IC contains four 2-input NAND gates as shown in Figure 2. Digital signals are either high (+V) or low (0V). The output of a gate will switch between high and low depending on its inputs. The input-output behavior of a gate is defined by its truth-table. Table 1 is the truth-table for a 2-input NAND gate where A and B are the inputs and X is the output. A 0 represents low (0V) while a 1 represents high (+V).
The CD4093 has what's called Schmitt Trigger inputs. That means the value of the input voltage required for a 1 depends on whether the input is switching from low-to-high or from high-to-low. It's meant to guarantee a clean transition on the output but it also allows us to build a simple square-wave oscillator.
Suppose the output of the gate is high. Then the current through resistor R1 will charge up capacitor C1 until the input voltage on the gate is high enough to force the output low; call it Va. Then C1 will discharge through R1, but the switching threshold on the gate has dropped. So C1 will have to discharge to a voltage lower than Va; call it Vb. At Vb the output will go high, but now C1 has to charge back up to Va. The process repeats and generates a square-wave on the output of the gate. The frequency is determined by the RC time constant tau (T). T = R1 ? C1. Pushing the normally open switch SW shorts the input to ground and stops the oscillation.
JK Flip-FlopThe CD4027 is a dual JK flip-flop as shown in Figure 3. Each flip-flop has three inputs: J, K and CLK (clock), and two outputs: Q and Q\ (Q\ is read as "Q BAR"). When Q is high, Q\ is low; when Q is low, Q\ is high. The operation of a JK flip-flop is described by Table 2. The column Qt is the output before a clock pulse while the Qt+1 column is the output after a clock pulse. The Xs in the table are called "don't cares" meaning they could be 1 (high) or 0 (low).
For this application, the key thing about a JK flip-flop is that when J and K are both held high, the outputs will toggle each time a pulse is applied to CLK. Toggle means to go to the opposite state. If it was high, it goes low. If it was low, it goes high.
LEDs and Transistor DriversCMOS logic devices consume little power and will work over a range of voltages (3V-15V). That makes them ideal for battery operation. Unfortunately, you can't get much current from their outputs (~1mA). But the current required to light an LED is 10mA to 20mA. To make this work, we will use a pair of MOSFET transistors as drivers for the LEDs. A MOSFET transistor is off until you turn them on by applying +V to the gate (G). Once on, the transistor will conduct up to 200mA from drain (D) to source (S). Figure 4 shows the 2N7000 MOSFET transistor.
Circuit BoardWe will build the circuit on a piece of proto-board as shown in Figure 5. The copper side is shown in 5(A) while the component side is shown in 5(B). This style board is ideal for mounting dual inline package (DIP) ICs; you mount them along the middle of the board.
Since we have only two ICs, we don't need the entire proto-board; so I snapped it into two pieces (about a 60-40 split). First, I scored through the copper side cutting the copper and scoring into the phenolic, not cutting all the way through. Once it was scored, I placed the board on the edge of a table with the score line along the edge, copper side up. Holding half the board flat to the table, with a quick motion I pushed the other half down to snap the board. The break wasn't perfectly clean, so I used a file to smooth it out. If all that seems like too much, just use the entire board without snapping it.
IC Sockets & Parts LayoutMistakes happen, and unsoldering an IC is tedious. So in this project we will use IC sockets. Refer to Figure 6 to see how the parts are arranged on the board, including jumper wires. Figure 7 shows how the parts and jumpers are soldered. Note that the leads of the switch need to be adjusted slightly to fit the holes in the board.
Copper SideFigure 7 shows the two strips of copper that run along the top and bottom of the board. They are used for +9V (red wire on battery clip) and ground (black wire). Drill a 1/8" hole between the two existing holes. Then feed the battery clip wires through and make a small knot. Then feed the black and red wires through the other two holes as shown. In Figure 6 you can see the red and black wires inserted into holes and soldered on the copper side. Note the bare-wire jumpers, including the one between the two ICs which brings +9V to pins 5 and 6 of the CD4027.
Wiring the SwitchSince I cut the board down, it turned out that the switch was connected to the same strips of copper as pins 4 and 7 of the CD4093. So I had to cut two of the copper traces to isolate the switch from those pins. Be careful to insert the switch correctly. Use an ohmmeter to identify the normally open contacts.
IC Pins UsedOn the CD4093, input pins 1 and 2 and output pin 3 are used for the oscillator; pin 7 is ground and pin 14 is +9V. On the CD4027, pins 1 and 2 are used for Q and Q\; pin 3 is the input (CLK); pins 4 and 7 (RST and SET) are connected to ground; pins 5 and 6 (K and J) are connected to +9V. Pin 8 is ground and pin 16 is +9V.
Time ConstantThe exact values of R1 and C1 are not critical. I used 100kΩ and 0.01µF to get T = 1ms. Using 0.001µF for T = 0.1ms would also work. Feel free to play around with values, but if the time constant gets too big, the LEDs will blink instead of being on steadily.
Final InspectionInsert the ICs into their sockets. Be careful to not put them in the wrong way. (You may have to squeeze both sides of an IC to get the pins to go into the socket.) Before applying power, use an ohmmeter to verify there are no shorts between +9V and ground. Verify that the transistors and LEDs are in the right way. Verify that the jumpers connect to the correct points. Look for bad solder joints, missing solder joints and shorted solder points. Verify that the red and black wires of the battery connector are soldered to the right places.
TestingIf it passes final inspection, connect a 9V battery. The two LEDs should immediately light. Press the switch and verify one LED is off and the other is on. Repeat the process several times to verify that the color of the lit LED is red or green at random.
Wrap UpWell, that's about it. Have fun with it. Remember to disconnect the battery before putting the circuit away.
About Nuts & Volts: Nuts & Volts is a monthly publication devoted exclusively to electronics topics such as circuit design, lasers, amateur robotics, computer control, home automation, microcontrollers, data acquisition, new technology, DIY projects, electronic theory, etc. Nuts & Volts is written for the hands-on hobbyist, design engineer, technician and experimenter. The diversity of subjects appeals to all levels of experience but focuses more on the intermediate to advanced level reader.