Thursday, 26 May 2016

Raspberry Pi Pulse Generator - Console Based

Background

I have a number of older digital integrated circuit components (both TTL and CMOS), and as part of my on-going experiments I wanted to create a simple counter and single 7-segment display. I needed to confirm that the counter could take in a number of pulses and provide a digital display readout. The pulses to be counted could come from a number of sources such as clocks or sensors providing a pulsed output. I ended up putting together a simple Python script on the Raspberry Pi to use as a pulse generator.

Research


Initially I put together the circuit below comprising a counter, with a binary to decimal coded LED driver feeding a 7-segment display.  There are many similar circuits available on-line, the operation and configuration of which is well documented, so I don't intend fully describing the operation. Suffice to say for each single complete pulse on the input to the counter, the output count is coded in order to drive, or light up, a series of LEDs arranged in the 7-segment display format to display digits 0 to 9.
For a detailed explanation and examples see the Digital Counter reference below.


To test the circuit I initially provided clock input to the counter using the simple push button pulse generator described in a previous article. This worked very well but was limited to single pulse operation, generated by hand.

To provide a more realistic input I decided to produce a simple Python script to run on a Raspberry Pi computer. This script would enable me to input pulse characteristics such as pulse length, the time between pulses and the number of pulses. Each pulse or set of pulses would appear on a General Purpose Input Output port (GPIO) and be connected to the digital counter input. The benefits of this would allow me to reliably generate noise free, repeatable pulses.

Operation

The diagram below shows the Raspberry Pi 26-way GPIO connector, with a connection from GPIO port 17, (pin 11) to an IC4050 hex buffer, which in turn connects to the input of the divide by 10 counter IC7490. The 4050 operates at 3.3v whilst interfacing to a 5v circuit, which is well within its operating parameters. It  is required to provide an interface between the Raspberry Pi GPIO output which operates at 0/3.3v, and the 7490 operating at 0/5v. Power for the 4050 can be taken from the Raspberry Pi as there is only one hex buffer in use. There are other configuration connections that need to be put in place associated with the 7490 and 7447; see http://www.electronics-tutorials.ws/counter/bcd-counter-circuit.html



The Python script below runs in a console / terminal display and takes in user supplied parameters to generate pulses - a series of High and Low level changes. The script is commented which should make it reasonably straight forward to understand what's going on. The script isn't entirely error proof - this was one of my first Python coding attempts using the Raspberry Pi GPIO ports, but if you are careful in selecting the input parameters all should be well.

In operation I have found this script to be very useful, not only with this simple counter application but with experimenting with other shift register and counter combinations where larger numbers of pulses are required. The advantage of using such a script on the Raspberry Pi is the ability to craft an exact pulse stream. Where the circuit under test could produce unknown results it's most important to have confidence in the input pulse source.

# Python v3.4.2 console based script
# Set an individual Raspberry Pi GPIO port as an output
# Generate pulses by setting the port High & Low [amount of pulses user supplied]
# Pulse duration/length & inter-pulse duration/length is user supplied
# *** NOTE no check is made whether the port number entered is valid to use as an output ***
# Selected port is set Low prior to 'pulse' generation
# Tested on RPi Model B & 2

import RPi.GPIO as GPIO # import GPIO module, use local name GPIO to reference
import time


GPIO.setwarnings(False) # disable warning messages
GPIO.setmode(GPIO.BCM) # use chip specific GPIO pin numbers

try:
    while True:
        print ("Set Raspberry Pi GPIO port as output & generate pulses")
        print ("Warning! supplied parameters are not validated")
        print ("Enter a valid Raspberry Pi GPIO number to set as an output [99 quits]")
        portstr = input(">_ ") # get port number as a string
        print
        port = int(portstr) # convert string to integer
        if port != 99:
            GPIO.setup(port,GPIO.OUT) # set port as output
            GPIO.output(port,GPIO.LOW) # set port low to start
            print ("Enter length of pulse in seconds") # get pulse length
            lengthstr = input(">_ ") 
            length = float(lengthstr)
            print ("Enter inter-pulse time in seconds") # get pulse off time
            spacestr = input(">_ ") 
            space = float(spacestr)
            print ("Enter number of pulses") # get number of pulses to generate
            pulsesstr = input(">_ ") 
            pulses = int(pulsesstr)
            loop = 0
            while loop < pulses: # set port High & Low, generate pulses
                GPIO.output(port,GPIO.HIGH)
                time.sleep(length)
                GPIO.output(port,GPIO.LOW)
                time.sleep(space)
                loop += 1
        else: # user exit
            print ("Exiting..")
            GPIO.cleanup()
            break

except KeyboardInterrupt: # catch the Ctrl-C
    print ("Exit..")
except: # catch other errors
    print ("Error..Exiting")
finally: # clean up on exit
    GPIO.cleanup()


Python script in operation


The screen shot below shows the parameters provided to generate a series of pulses. In this case each pulse comprises an on/High period of 100mS and an off/Low period of 900mS. Total pulse length being 1 Second.


Pulse outputs - examples

To illustrate a number of pulse outputs I have included some annotated oscilloscope traces with various user input values:

First trace: pulse on/High period 100mS, off/Low period 900mS.




Second trace: 50 pulses, each having pulse on 1mS, pulse off 9mS.



Third trace: an expansion of the 50 pulses train to show the pulse quality and timing accuracy. There is a slight increase in time between pulse periods - this seems to be more apparent as the pulse lengths decrease, with frequency increasing. Never the less, for experimenting purposes where absolute timing accuracy isn't the primary requirement, I've found this to be more than adequate to drive counter circuits.





Components
IC 4050 Hex buffer
IC 7490 Divide by 10 counter
IC 7447 7-segment display driver
7-segment display LED, common anode
7 resistors; 220Ω 1/4w

References:
IC 4050 Hex buffer datasheet

Digital Counter

IC 7490 Decade and Binary Counter

IC 7447 BCD to 7-Segment Decode/Driver

7-Segment Display - Common Anode

Thursday, 19 May 2016

Pulse Generator

Background

Sometimes when experimenting with or testing digital logic circuits a manual switching function is needed to momentarily take an input 'high' and then return to 'low' state. This could be to simulate a logic change on a digital input, or to act as a switch on one input of a dual gate device to enable another input to pass through the gate. See below for examples:


Manual input toggle high/low to simulate logic level change:



Manual gating to enable another input through the circuit:


However, using manual switches can produce unreliable and unpredictable results due to switch bounce.

Research

Using the example of the manual switch de-bounce circuit at labbookpages I experimented with various component values to produce a push switch circuit capable of generating a single pulse approximately 0.275 seconds (approximately 250mS). This time period was sufficient for a number of experiments I wanted to try involving detecting logic level changes. This time period was also useful when I was testing the use of a clock signal to pass through counting circuitry at a relatively slow speed.

The circuit below comprises a switching and timing function, and in combination with a Schmitt Trigger device (IC4584) produces reliable, repeatable and good quality pulses. In essence a Schmitt Trigger is used to 'square up' slowly changing waveforms or inputs, and is ideally suited to providing a clean and sharp switched output in response to the relatively slowly changing input conditions in the circuit below.

Switch & Timer


Operation

In the circuit above, when power is applied capacitor C1 charges via R1 and D1. When sufficient voltage is present on the inverting Schmitt Trigger IC1 input, the output will be 0V (logic 0).

When switch SW1 is pressed C1 discharges via R2, the voltage on IC1 input will be near to 0V, therefore IC1 output will be +V (logic 1).  The output remains at +V until SW1 is released and C1 charges to the voltage needed for the Schmitt Trigger to switch it's output to 0V.

With a momentary press of SW1 and using the combination of R1, R2 and C1 above, the generated pulse length is 275mS.

Using some alternative values of C1 produces pulse lengths:
4.7uF = pulse duration 860mS
9.4uF = pulse duration 1300ms

For more information on the operation of this circuit see labbookpages.co.uk

For clarity purposes in all circuit examples I have not included the positive or negative connections from each integrated circuit to +V and 0V. Each IC datasheet provides pin-out descriptions and numbering.


Switch & Timer with external interface and indicator


With additional components I modified the original circuit above to provide buffered outputs to drive external logic circuits and a visual pulse indicator. The 4584 Schmitt Trigger (IC1) can supply up to 10mA on each output which maybe inadequate to directly drive external logic circuitry and an LED. I used the non-inverting hex buffer 4050 (IC2) to provide two outputs as shown below. I prefer to provide buffering when interfacing to potentially unknown logic inputs and LEDs to help prevent damage occurring. R3 prevents unnecessarily high current flow. C1 provides power supply decoupling to prevent power supply 'noise' causing any erratic circuit operation. I have successfully tested and used this circuit powered at 3.3v and 5v which makes it very useful to interface with single input TTL, CMOS, Raspberry Pi and Arduino based circuitry. Additional buffering of the 4050 output is necessary if driving multiple circuits.


Components

R1 100kΩ 1/4w
R2 4.7kΩ 1/4w
R3 2.2kΩ 1/4w
C1 100nF
C2 3uF
D1 1N914 (or similar general purpose low voltage diode)
D2 Red Led
IC1 4584 Schmitt Trigger (6 channels)
IC2 4050 Hex buffer (6 channels)
  

References:

Decoupling

Switch bounce

IC 4584 Schmitt Trigger datasheet

IC 4050 Hex buffer datasheet


Tuesday, 17 May 2016

A Latch - Set & Reset


Background

I have experimented with various logic circuits within the 74xx and 4000 integrated circuit series, the Raspberry Pi and an Arduino clone using directly connected switches to simulate logic level changes. In some cases this is to 'preset' a logic level on a particular input, to simulate a logic level change on another input, or to act as a 'gate' function to control a particular logic function. Due to the phenomenon of something known as switch 'bounce', unwanted or unexpected results can occur. In some cases when using microprocessors or micro-controllers, code can be written to mitigate the effects of switch bounce. However, this isn't alway practical or possible if switch connections need to be made directly to logic circuits.

Research

Using various information sources in reference books and on-line, I put together this circuit below, based around the 555 'timer' device. I claim no originality for how the 555 is being used, but through experimentation I used some additional discrete components to provide two 'logic' outputs to allow interfacing to both 3.3v and 5v logic circuitry.

Operation

When power is first applied, output 1 (o/p1) is 'low' at 0v. This 0v level is also applied via R6 to the Base of Q1 turning  the transistor 'off'. The voltage present at the Emitter/R5 junction and therefore output 2 (o/p2) is 0v.

When Switch 1 (SW1) is pressed and released, pin 2 of TLC555 is taken low momentarily, the result of which sets pin 3 high at 5v. This logic 'high' level can be directly connected to TTL, CMOS or other +5v digital circuitry - ensuring that the current draw from the 555 doesn't exceed the specification, typically 100mA maximum.

The Led (D1) illuminates to confirm the transition on pin 3 from 'low' to 'high', with R3 preventing unnecessary current draw. R6 provides transistor base current limitation as Q1 is forward biased and switches on. Output 2 becomes 3.3v due to the voltage divider effect of R4 and R5. This o/p is suitable for direct connection to digital devices operating at 3.3v such as the Raspberry Pi and Arduino, current draw is limited by R4. The addition of Q1 and associated resistors provides separation between o/p1 and o/p2.

The Led remains illuminated and both outputs remain 'high' until Switch 2 (SW2) is pressed. Pin 6 is taken low resetting the latch, after which pin 3 goes low, the Led goes off and both outputs return to 0v.

Voltage measurements - no load attached
Output 1 is either 0v or 5v
Output 2 is either 0v or 3.3v

Current measurements - no load attached
Total measured supply current:
Latch 'set' : 1.4mA
Latch 'reset' : 0.11mA
Led current 0.94mA with Latch 'set'
Q1 Collector-Emitter 0.25mA

Components
R1, R2 1MΩ 1/4w
R3 3.3kΩ 1/4w
R4 6.8kΩ 1/4w
R5, R6 10kΩ 1/4w
C1 100nF
D1 Red Led
Q1 BC337 (or similar general purpose NPN transistor)
IC1 TLC555 (or similar low power NE555 type timer)

References:
7400 TTL series

4000 CMOS series

Switch bounce

555 Tutorial

Touch On-Off circuit [50 - 555 Circuits - Talking Electronics] http://www.talkingelectronics.com/te_interactive_index.html

TLC555 Datasheet

Introduction

I have had an interest in radio and electronics for many years, starting as a youngster taking things apart and investigating all manner of components. I have also experimented with equipment, circuits and components across amateur radio, basic electronics, constructing computers in the early days and with integrated circuits during the emerging digital era of the 1980's. In the late 70's I had formal training to operate and maintain all manner of marine radio and radar equipment - giving me a good grounding in radio and electronics.

Now I have a renewed interest in micro-computers and micro-processors such as the RaspberryPi and Arduino types. I'm having fun getting reacquainted with some older technology that I've managed to salvage, together with the more modern stuff. Also learning to code in Python has given me an idea to share some of the things I’ve been doing.
There are so many really good information sources and guides available on-line that I have used and appreciated. So now is a good time to try and put something back, something I hope will be of interest to experimenters and makers.
To the experienced technologist I suspect some of my experiments might seem basic. I have found that some articles on-line leave me with questions about how a particular circuit is working, why it does what it does, or perhaps the code hasn't been sufficiently documented and leaves me scratching my head wondering what’s going on.
In the posts that I hope to provide I'll try my best to provide enough supporting information.
Get in touch if you have any comments or suggestions.

My first article follows on from this intro and is a basic Latch [set & reset] circuit.