Microbit Function Generator
by Gammawave in Circuits > Microcontrollers
8809 Views, 60 Favorites, 0 Comments
Microbit Function Generator
Waveform generation may not immediately spring to mind with regard to the Microbit as it is a digital device.
However, with the addition of some external logic, waveforms other than pulses can be generated.
This will be realised with a Function Generator, which is a signal generator that produces different types of waveforms.
In this case these waveforms will be sine, sawtooth and pulse with the pulse frequency adjustable over the range 0 to 1MHz.
This project is an enhancement to an earlier project previously published:
Microbit Pulse Generator - Variable Ranges
The enhancements are a custom designed PCB to carry out the D2A conversion for the Sine and Sawtooth waveforms complete with independent outputs and independent amplitude control.
With the addition of an OLED display to indicate range, frequency and output status.
All housed in a custom 3D printed enclosure.
Supplies
MicroBit V1 or V2
SPST Momentary Switch - Qty 2
Dual 4mm Banana plug socket - Qty 3
Potentiometer 5k - Qty 2
Potentiometer Knobs 6.4mm shaft - Qty 3
Jumper Wires M/F - Qty 23
15k Resistor* - Qty 3
27k Resistor* - Qty 3 (Closest available value to 30K)
57k Resistor* - Qty 3 (Closest available value to 60k)
120k Resistor* - Qty 3
100k Resistor
200R Resistor - Qty 2
150R Resistor
330R Resistor
*Resistors 1% tolerance or better.
100nF Capacitor - Qty 6
CD4024 (7 stage Binary Counter)
CD4015 (Dual 4 bit shift register)
CD4030 (Quad 2 input EXOR) alternatively an CD4070 which has the same pinout and function.
MCP602 (Dual Op Amp)
2pin PCB Terminal Strip - Qty 6
14Pin DIL Socket - Qty 2
M3 x 8mm (screws/bolts) - Qty 32
M3 x 10mm (screws/bolts)
M3 x 30mm (screws/bolts) - Qty 2
Clear/Transparent Perspex - 52mm x 52mm x 5mm
2 part epoxy resin or paint for the text inlay (Optional)
Tools
Pointed nose pliers
Cutters
Soldering Iron
Solder
Screwdriver
Taper Drill 2mm -12mm
Needle files
3mm drill bit
3d Printer
Fine modelling blades
Cocktail sticks or Skewers
What Is D2A Conversion
D2A is the process of converting a digital signal into an analogue signal
A digital signal is binary in nature occupying two distinct logic states of high and low or with worst case voltages of ~3V* and 0V with reference to the Microbit.
*(The supply voltage tolerance means that the voltage will vary between Microbit's and for the exact voltage should be measured with a DMM and this substitute in calculations.)
Other logic systems have different equivalent voltage levels that represent logic high or low.
In reality rather than a specific voltage being used to define a logic input state its defined from a threshold value.
A High (1) level >=VIH (70% of 3V) and Low (0) level <=VIL (30% of 3V)
Where VIH =Voltage Input High, VIL = Voltage Input Low
By comparison a true Analog signal can have an infinite number of values and varies continuously with time.
The Analog signals will be sine and sawtooth waveforms.
But how will the conversion for each of these waveforms be accomplished.
Digital Sawtooth
A digital sawtooth waveform can be generated using a binary counter with weighted outputs.
Using a 4 bit binary counter we can generate 16 discrete steps
With a resistor R on Q0 a current flows when Q0 is high.
If we place a resistor on Q1 that's R/2 then twice the current will flow when Q1 is high.
Consequently, if we fit resistors of R/4 on Q2 and R/8 on Q3 then we have a binary weighted current for each binary step.
The resistor values following an ideal weighting would be 120k, 60k*, 30k & 15k.
*No preferred resistor value exists for 60k as a single value the closest is 59.7K (E96/E192 series), however it can be made simply with two 30K in series.
If the summing point is connected to a load resistor each current step generates a corresponding voltage step.
The frequency of the output waveform = Fin/2^n where n = weighted outputs.
Therefore if Fin = 1KHz then the sawtooth frequency will be 1000/16 = 62.5Hz
Digital Sinewave
A digital sinewave waveform can be generated using shift resisters with weighted outputs.
Using two 4 bit shift registers we can generate 8 discrete up steps and 8 discrete down steps for symmetry.
For the first shift register.
With a resistor R on Q0 a current flows when QO is high. If we place a resistor on Q1 that's R./2 then twice the current will flow when Q1 is high. Consequently, if we fit resistors of R/4 on Q2 and R/8 on Q3 then we have a weighted current for each step counting up.
For the second shift register.
With a resistor R on Q3 a current flows when Q3 is high. If we place a resistor on Q2 that's R./2 then twice the current will flow when Q2 is high. Consequently, if we fit resistors of R/4 on Q1 and R/8 on Q0 then we have a weighted current for each step that reduces as the count increases.
If the summing point for both counters is connected to a load resistor each current step generates a corresponding voltage step.
The frequency of the output waveform = Fin/2^(n/2) where n = weighted outputs.
Therefore if Fin = 1KHz then the sinewave frequency will be 1000/16 = ~62.5Hz
Circuit Design
The circuit is designed using low voltage logic IC's (Shift register, Binary Counter, EXOR's and OpAmp) using DIL packages for ease of assembly.
The Sawtooth waveform is generated using a CD4024, 7 bit binary counter with binary weighted resistors connected to the 4 LSB outputs.
The clock input is derived from the MicrobIt at output P0.
The control for the clock frequency is provided by a 100k, 10 turn potentiometer with the main resistance connected between 0V and 3V and the wiper connected to P1 of the Microbit to provide a variable voltage (Vv) that is used to vary the frequency.
Vv = Rx/100k*3V where Rx is the resistance between the wiper and the connection to 0V
A POR (Power On Reset), is provided by a CR* network connected to the reset pins to ensure the Counters and the Registers power up in the low state.
*(The CR network pulls the Reset pin High on power up for ~1mS until the capacitor charges then its maintained low by the resistor).
The Binary Counter increments on each negative transition of the clock advancing one count from 0 to 15 then resets to 0 to repeat the cycle.
The result is a repetitive digital sawtooth wave.
The voltage at the summing point is fed to the non inverting input of one half of a dual Op-Amp configured as a non inverting amplifier.
A 5k (Rf) potentiometer is connected in the feedback path in conjunction with a 220R (Rp) to control the waveform output amplitude. Vgain = 1+Rf/Rp with Vgain from 1 to 24 enabling the output voltage to be controlled from ~0 to 3V.
The Shift Register transfers data on each positive transition of the clock and the last stage (Q3) of register 1 is connected to the data input of register 2 with its last stage (Q3) fed back to the data input of register 1.
At switch on all the registers are reset to zero but the inverter connected to the data input of register 1 sets each stage to a logic 1 on each clock transition. Giving a progressive increase in voltage.
Once the last stage of register 2 is set to 1 the data input is set to 0 by the inverter and each stage is successively set to 0. Giving a progressive reduction in voltage.
The result is a repetitive digital sine wave.
The voltage at the summing point is fed to the non inverting input of one half of a dual Op-Amp configured as a non inverting amplifier.
A 5k (Rf) potentiometer is connected in the feedback path in conjunction with a 220R (Rp) to control the waveform output amplitude. Vgain = 1+Rf/Rp with Vgain from 1 to 24 enabling the output voltage to be controlled from ~0 to 3V.
Coding
In the original incarnation of this project the Microbit display was used to indicate status.
However, as the Microbit is limited in the information that can be displayed in one go without resorting to scrolling this was upgraded to an OLED display.
Therefore additional code was included to make use of this display.
The information displayed is:
Application Name and version.
Output status.
Frequency Range
Frequency
Code description
On start
This calls reset (variables assigned and initial values set). If using Microbit V2 which has a built in sounder this will need to be switched off, ignored if using V1.
Button A - Output Enable
Displays output enabled or output disabled
Default on start up is output disabled.
Button B - Range
Sets the max_range on each button press for 1k, 10k, 100k, 250k & 500kHz max_range.
Default on start up is 1kHz.
Pot_Adjust
Reads the analogue input on P1 Converts it into a percentage of the maximum bit count and into a frequency based on the maximum range. Frequency = max_Range * (bit value/1023)
Update the display value relative to the selected max_range
Forever
Where the ring tone is applied with the required frequency.
Output enable is applied here subject to its status to output the required frequency or zero frequency.
In the code much of the activity to the Microbit display is disabled as it will be enclosed inside the box.
However, this can be re-enabled simply be reinserting the Plot code block.
Downloads
Enclosure Design
For previous projects were I have made boxes these were made in two parts (an open box and a lid), but for this project there was a departure from this method being much larger than previous builds, I decided to make six sides to enabled them to be 3D printed separately.
The enclosure was designed in TinkerCAD: Function_generator_box
All fixing holes, cut outs, standoffs and legends would be created as part of the printing process.
The size of the completed box is 132mm (W) x 93mm (H) x 104mm (D) and consists of Top, Bottom, Left, Right, Back, Front & Display supports.
Enclosure Printing
The individual elements for the enclosure were 3D printed with the following settings.
Layer Height: 0.15mm
Infill Density: 25%
Infill Pattern: Tri Hexagon
Base Adhesion: Brim
PCB Assembly
For ease of assembly mount the components with the lowest profile first progressing to those with the highest profile last.
As this enables you to solder the components in on a flat surface which helps to hold them in place.
Resistors.
Bend the leads of the resistors at both ends with pointed nose pliers and insert the correct value between the holes in the board. The values are printed on the board to reduce errors in assembly.
Solder the wires to hold in place.
Sockets
Insert the IC sockets paying attention to the pin 1 identification and solder in place.
Ceramic Capacitors
Bend the wires with pointed nose pliers if applicable to fit between the holes and prevent excessive stress on the component body and solder in place.
Electrolytic Capacitors
Bend the wires with pointed nose pliers if applicable to fit between the holes and prevent excessive stress on the component body and solder in place.
Terminal Blocks
Insert the pins through the board and solder in place.
Before inserting the IC's visually check the board to ensure all the connections are soldered and that no solder bridges exist and/or with a DMM (on diode or resistance), check between + and - on the power terminal to ensure there are no short circuits.
Once you are confident that no shorts or opens exist and that all the passive components are correctly placed its time to insert the IC's again making sure that they are correctly orientated and inserted into the correct socket.
The names are printed on the board to reduce errors in assembly.
Ensure ESD protective precautions are followed to prevent damage to the IC's during handling and insertion.
The Gerber files for PCB creation can be found here:
Box Assembly
There are a number of elements involved in the box assembly.
Starting with the base the Breakout board is screwed to the shortest pillars. These are already printed with through holes allowing self tapping screws (M3 x 8mm max.), to be inserted from the top or bolts (M3 x 9mm min), to be inserted from the bottom and a nut fitted on the top.
M/F 115mm jumper wires connect the Breakout board to the PCB terminal block and Display board.
The connections from the Breakout board to the PCB are:
P0 to CLK IP
P1 to FCTRL IP
3V to + & 0V to -
Prior to fitting the PCB the OLED display needs to be connected.
However, for this project the main edge connector is bypassed and right angle terminal pins are soldered to the I2C expansion port.
The connections from the Breakout board to the Display board are:
SCL (19) to SCL
SDA (20) to SDA
3V to 3V
0V to 0V
The connections to the switches are:
Enable (Button A- P5)
Range (Button B -P11)
With the remaining connections for these two switches connected together at 0V.
The PCB is fitted to the 27mm pillars with self tapping screws (M3 x 8mm) or bolts (M3 x 30mm).
Proceed to connect the terminal posts, switches and potentiometers to the front panel.
The pre-existing holes may need deburring or adjusting in size if different elements are used this can be accomplished with a file or drill as required.
The front terminal posts are connected to the PCB at the terminal blocks.
The potentiometers are connected to the terminal pins on the PCB and an anti turn hole may need to be drilled at the rear of the front panel panel as this is not pre-printed.
Fit knobs appropriate to the potentiometers.
Once the front panel elements are fitted the display needs to be set in place.
Included within the box elements are two display slots these attach to the back of the front panel and hold the display in place.
The upper holes in the slots only sit over the protruding edge of an M3 x 8mm nut and bolt whilst the lower holes are held in place by a nut and bolt that passes all the way through the front panel and the slot.
However, due to the position of the connector across the top of the display board the display is set back from the opening in the front panel creating a gap. This gap is taken up by a transparent piece of Perspex 52mm x 52mm x 5mm in size.
At 26mm x 5mm along the bottom of the Perspex block a 3mm hole both in the Perspex and coincident with the front panel is made and held together with an M3 x 10mm bolt.
The right hand side panel has a 8mm hole to accommodate the USB plug but this may required widening subject to the size of the plug body.
The box can be fully assembled using the pre-printed holes which align with their neighbouring edges and fixed with M3 x 8mm self tapping screws.
Text Inlay
The text is included as part of the 3D printing process for permanency and with the intention that each character will be filled in to create an inlay.
Prior to filing in the characters some preparation may be required to remove excess material as a result of over extrusion which may create islands or barriers in the character. Use a scalpel or picking tool to remove these.
The characters could be filled in with a variety of materials, paint, correction fluid, nail varnish or liquid epoxy resin. In this case epoxy resin will be used which will be a 2 part 1:1 mix.
Epoxy resin pigment is added once the resin is mixed which for this project is a white pigment.
Depending on the viscosity, the area and level of detail, drying time and ease of cleaning surfaces and material different application techniques can be employed.
These may range from syringe, paint brush, spreader or dropper.
The dropper technique using a skewer or cocktail stick will be employed were the stick is dipped into the resin and small drops of resin are transferred to the depression which will hold the resin in place once dry.
Apply the drops in small quantities and build up the filling, if you over fill; the excess can be removed with a tissue, cotton bud paint brush or skewer.
The greater the percentage of infill the smaller any bleed through around the character due to the reduction in voids. This bleed through my result in the resin being sucked out of the character being filled.
You could repeatedly fill the character(s), or refill once the resin has set filled in local voids which my prevent further bleed.
Once all the letters are filled in leave horizontally until the resin has cured.
Another example of this technique can be found in a previous project: Micro Binary Clock
Operation
Power the Function Generator from a suitable power source via USB, the connector opening is on the right hand side.
It will initialize and display the status.
This will be Output disabled, Max frequency range: 1000Hz, Frequency 0Hz.
There will also be a green glow from the power indicator visible around the display.
Press the Enable button to turn on the outputs and the pulse frequency will be displayed.
Press the Range button to change the maximum frequency range.
Turn the frequency knob to adjust the frequency value.
Adjustment of the frequency will effect the sine, sawtooth and pulse waveforms.
However, sine & sawtooth will have a frequency 16 times lower than that at the pulsed output due to the D2A conversion process.
The sine and sawtooth waves have independent amplitude control from ~100mV to 3V.
The pulse output voltage is fixed at 3V maximum.
However, a potential divider on the output will allow setting of different voltages using the formula.
Vout = 3V*Rout/Rtotal where Rtotal = Rin+ Rout in series from the output to 0V, with the output taken from the centre tap of the two resistors.
If Rin =10K and Rout = 5K then 3V*5K/15K = 1V maximum.
Connections to the output terminals can be made using 4mm banana plugs, spade connectors or loose wires.
Additionally, the spacing between the output terminal posts is compatible with a BNC to 2x 4mm banana plug adaptor allowing the use of Co-axial cables if required.