DC, (Direct Current), Input For Text Mode Python 3.x.x.
A kids level project to do for yourselves...
This is a Python DEMO to show the power of the sound card using Linux to accept a DC, (Direct Current), input. It is a kids level project that uses the sound card microphone input as a detector for an audio VCO output.
Written in such a way that anyone can understand how it works...
The DEMO circuits inside the code are SOOO simple a dexterous 10 year old could make it in less than a couple of hours and have it up and running so as to start using it... The DEMO code is a simple, slow, positive logic, TTL level Logic Probe.
(My own VCO circuit is almost as simple but has 5 transistors.)
This is for Linux and Python 3.x.x. Read the code for much more information, and......
Enjoy finding simple solutions to often VERY difficult problems...
Bazza, G0LCU...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | # DC_IN.py
#
# This DEMO shows how to get DC, (Direct Current), into a computer without
# the need for Integrated Cirduits, USB, Serial, Parallel etc, etc...
# It is used in this code as a very simple Logic Probe that will give some
# indication of a Logic 0, 1 or indeterminate. Only the external microphone input
# is required. "/dev/dsp" IS required so install "oss-compat" from your distro's
# repository if you do not have "/dev/dsp"...
# Ensure the sound system is not in use, and, use the OS's mixing facilities to
# set any input and output levels...
# Tested on Debian 6.0.0 using Python 3.1.3 and PCLinuxOS 2009 using Python 3.2.2.
# (C)2010, B.Walker, G0LCU. Now issued as Public Domain.
# Written in such a way that anyone can understand how it works.
#
# A very simple VCO, (Voltage COntrolled Oscillator), can be found here...
#
# http://www.4qdtec.com/mvbz/vco2.gif
#
# Assume a supply rail of 5 Volts to the VCO along with the circuit of the probe below...
#
# +5 Volt rail on VCO, (Vcc).
# O-------------------------+
# |
# | +
# --+--
# D2. / \
# +---+
# |
# 1-4 Volt VCO I/P, (Vc). |
# O--------o---/\/\/\---o---o---/\/\/\---0 Probe I/P.
# | R2. | R3.
# O < | +
# | > --+--
# | R1. < / \ D1.
# | > +---+
# | < |
# | | |
# +--------o------------o-------o--------O -VE.
# 0 Volts. |
# ---+--- GND.
# Parts List. ///////
# -----------
# R1 = 1 MegOhm.
# R2, R3 = 470 Ohms.
# D1, D2 = 1N4148 Diodes.
# All tolerances are wide open.
# Sundries, stripboard, wire, etc...
def main():
# Make variables global; my choice... ;o)
global record
global n
global freq
global logic
global LED
global colour
# Set the startup values...
freq=0
record=b"?"
n=0
logic="0"
# Use "H" for this DEMO although the commented out "LED" may look better.
LED="H"
# LED=chr(0x2588)
colour="\033[1;32m"
while 1:
# Do a 1 second recorded burst...
audio=open('/dev/dsp', 'rb')
# "record" is the "binary string" to be counted...
record=audio.read(8000)
audio.close()
# Enter another loop to do the count...
n=0
freq=0
while 1:
# A VCO with a mark to space ratio of 1 to 1 will be used for this DEMO,
# so "wait" until a "space" is found.
# (For those that don't know.)
#
# +------+ +---
# Square wave:- | Mark |Space |
# ---+ +------+
#
# This ensures that the loop cycles when NO input is
# applied to the microphone socket.
# Exit this loop when "mark" is found or n>=8000...
while record[n]<=127:
n=n+1
# Ensure as soon as n>=8000 occurs it drops out of the loop.
if n>=8000: break
# Ensure as soon as n>=8000 occurs it drops completely out of this loop.
if n>=8000: break
# Now the "mark" can loop until a "space" is found again and the whole
# can cycle until n>=8000...
while record[n]>=128:
n=n+1
# Ensure as soon as n>=8000 occurs it drops out of the loop.
if n>=8000: break
# Ensure as soon as n>=8000 occurs it drops completely out of this loop.
if n>=8000: break
# "freq" will become the frequency of a symmetrical waveform
# when the above loops are finally exited, n>=8000...
# Tick up the freq(uency) per "mark to space" cycle.
freq=freq+1
# Just 3 levels are displayed here but with more "if" statements much more
# accuracy and range is easily possible. Also "look up tables" could be used if desired...
# Set colour to Green for Logic 0, Red for Logic 1 and Yellow for indeterminate.
# Logic 1 is approximately greater than 4 Volts.
if freq>=3000:
logic="1"
# Red...
colour="\033[1;31m"
# Logic 0 is approximately less than 1 Volt.
if freq<=300:
logic="0"
# Green...
colour="\033[1;32m"
# Indeterminate is between 1 and 4 Volts and/or a slow oscillation being measured...
if freq>=301 and freq<=2999:
logic="indeterminate"
# Yellow...
colour="\033[1;33m"
# An ultra simple clear screen line...
# This line is not needed for the demo but added for fullness...
print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
# Now display the value in the same place on screen every time.
# This assumes a 24 or 25 line Python Shell window. Just modify to
# suit your particular Shell that you use...
print("\033[0m\nSimple DC Input in the guise of a TTL level Logic Probe.\n")
print("(C)2010-2011, B.Walker, G0LCU. Issued as Public Domain.\n\n\n")
# Print a large coloured square "LED" for quick and easy viewing.
for n in range (0,3,1):
print(" "+colour+LED+LED+LED+LED+LED+LED)
print("\033[0m\n\n\nLogic level is "+colour+logic+"\033[0m...\n\n\n\n\n\n\n\n\n")
main()
# End of DC_IN.py DEMO.
# Enjoy finding simple solutions to often very difficult problems.
|
Another kids level piece of code that anyone can understand...
You MIGHT have to AC couple and attenuate the VCO output from one of the oscillator transistors.
NOTE:- I have built the oscillator inside the code and it performs to specification.
Enjoy finding simple solutions to often VERY difficult problems...
Bazza, G0LCU...
This, revision 2, is NOT important......
I just noticed a few none important typos, so corrected them.
Also......
If your machine has an internal microphone then singing a note above 300 Hz will change the coloured sections to yellow. I suspect no-one can sing above 3 KHz so red is out of the question... ;o)
Bazza, G0LCU.