The MagPi - July 2018

(Steven Felgate) #1

Tutorial WALKTHROUGH


(^48) July 2018 raspberrypi.org/magpi
showed up as one of two ports. Comment out which
one is not applicable when defining the sampleInput
variable at the start of the listing.
Finally, we cobbled together a 168×78 pixel logo
for the top-left corner, using a piece of clip art
and fashioning the word ‘Oscilloscope’ from an
outlined version of the Cooper Black font. We called
it PyLogo.png and placed it in an images folder next
to the Python code.
Using the oscilloscope
The oscilloscope samples at 58 kHz, which in theory
means you can measure waveforms at 29 kHz. But
that only gives you two samples per cycle and as
the samples can be anywhere on the waveform,
they do not look very good. As a rough guide, you
need at least ten points on a waveform to make it
look like a waveform, so that gives a top practical
frequency of 5.8 kHz. However, by using the Time
Magnify options along with the freeze function, you
can measure much higher frequencies. The time and
voltage cursor lines let you find out the values on
any point of the waveform, and by clicking the save
functions the current cursor is replaced by a dotted
line which is fixed, and measurements can be made
relative to that. The oscilloscope in action can be
seen in Figure 2. Note that pressing the S key on the
keyboard produces a screen dump of the display.
Taking it further
There are lots of ways you can take this project
further. A simple upgrade would involve you having
a second data buffer to allow you to display a saved
waveform to compare against the current live one.
You could also add a lower speed acquisition mode
to see slower waveforms. You can go the other
way and use a faster Arduino so you can see the
higher frequencies. This oscilloscope is AC coupled;
you could add a DC coupling option with a switch
potential divider and amplifier to the front end to
extend the range of voltages you can measure. All
these improvements, however, will need changes to
the software to allow the measuring to take place on
these wider-range parameters.
int buffer [ 512 ]; // 1K input buffer
int sample, lastSample;
int pot 1 , triggerVoltage;
int triggerTimeout = 1000 ; // time until auto trigger
unsigned long triggerStart;
char triggerType = '2';
void setup(){
Serial.begin( 115200 );
pinMode( 13 ,OUTPUT);
// set up fast sampling mode
ADCSRA = (ADCSRA & 0xf8) | 0x04; // set 16 times division
}
void loop(){
if( triggerType != '2') trigger(); // get a trigger
digitalWrite( 13 ,HIGH);// timing marker
for(int i= 0 ; i< 512 ; i++){
buffer[i] = analogRead( 0 );
}
digitalWrite( 13 ,LOW); // timing marker
pot 1 = analogRead( 2 ); // switch channel to cursor pot
for(int i= 0 ; i< 512 ; i++){
Serial.write(buffer[i]>> 8 );
Serial.write(buffer[i] & 0xff);
}
// send back pot values for cursors
pot 1 = analogRead( 2 );
analogRead( 3 ); // next cursor pot
Serial.write(pot 1 >> 8 );
Serial.write(pot 1 & 0xff);
pot 1 = analogRead( 3 );
triggerVoltage = analogRead( 4 );
Serial.write(pot 1 >> 8 );
Serial.write(pot 1 & 0xff);
triggerVoltage = analogRead( 4 );
pot 1 = analogRead( 0 ); // prepair for next sample run
Serial.write(triggerVoltage>> 8 );
Serial.write(triggerVoltage & 0xff);
while(Serial.available() == 0 ) { } // wait for next request
triggerType = Serial.read(); // see what trigger to use
while (Serial.available() != 0 ) { // remove any other bytes
in buffer
Serial.read();
}
}
void trigger(){
// trigger at rising zero crossing
triggerStart = millis();
sample = analogRead( 0 );
do {
lastSample = sample;
sample = analogRead( 0 );
}
while(!(lastSample < triggerVoltage && sample



triggerVoltage) && (millis() - triggerStart <
triggerTimeout));
}




















































































































  1. Gather_A0.ino
    Figure 2 Taking measurements on a swept signal




Free download pdf