Using an ECIO as a USB analogue oscilloscope
Posted: Fri Oct 30, 2009 1:30 pm
Oscilloscopes are common place in electronics workshops and laboratories. However they can be expensive and in a lot of cases they are bulky and more than is needed for the job in hand. Oscilloscopes also come in the form of storage scopes, which can record an output trace over a much longer period of time.
Using an ECIO combined with the Flowcode USB and Analogue components it is possible to create a very low cost basic scope that will replicate most of the functionality from an oscilloscope. As the scope data is also going into a PC it is also possible to take this further. For example you could measure the ambient light level outside and then when it gets dark you could automatically shut your curtains.
The demonstration program uses the Flowcode USB Slave component to transfer the voltage data from the ECIO to the PC. The ECIO device sits in a loop waiting for communications from the PC. At the same time, the analogue channel is being sampled as part of a timer interrupt service routine. Each sample is placed into a data buffer consisting of an array of byte variables. When the request from the PC comes into the ECIO device the data buffer is copied to the outgoing USB buffer and the ECIO then starts again from the beginning of the buffer. It is therefore possible to change the timer interrupt rate and directly control the analogue sample rate.
The PC software was created using the free VB 2008 Express Edition and simply outputs the incoming data by plotting points on an x y graph. Each time the PC requests data from the ECIO it receives the complete buffer and straight away plots the buffer onto the graph. The graph can hold 600 values so the time frame for this is 600 multiplied by the ECIO timer interrupt period. The example comes with a timer interrupt rate of 732.4hz which corresponds to an interrupt period of 1/f = 1.365ms or a sample window of 600 x 0.00136 = 0.82 seconds.
The output buffer for the USB slave component is 32 bytes in length by default. I have used the custom code feature of Flowcode to modify the USB Slave component to allow for a much larger array size of 128 bytes. The USB Slave component uses the Defines section of code to define the USB buffer sizes. I have modified the default settings by finding the line shown below:
#define USB_EP1_IN_SIZE 32
And changing it to this.
#define USB_EP1_IN_SIZE 128
You will also need to rearrange the in and out buffers for endpoint 1 to allow the increased buffer to fit in with the other USB buffers.
Eg
#define USB_EP0_OUT_ADDR 0x0500
#define USB_EP0_IN_ADDR 0x0508
#define USB_EP1_OUT_ADDR 0x0510
#define USB_EP1_IN_ADDR 0x0530
The maximum buffer size that can be used on a PIC is 256 so if you want a really fast scope then you could possibly double the buffer size again to give even more data throughput. This has to be balanced with how fast the VB can collect and process the data so as to avoid gaps in the output signal trace. In an attempt to try and speed up the VB.net code I have used a technique called double buffering where the output data is plotted onto a hidden image. Once the data has been plotted onto the image the image is then loaded onto the screen. This is much faster then trying to plot the signals directly onto the screen.
To provide the analogue sources I have connected an EB003 sensor board to Port A of the ECIO and used the onboard LDR and potentiometer. Alternatively you could connect the analogue inputs to just about anything to read the voltage. You may want to put a 10K series resistor between the analogue input pin and the analogue voltage source to avoid any potential damage to the ECIO microcontroller. Here is a good example circuit that will suit most types of analogue resistive sensor.
It is also good practise to add a ceramic capacitor between the analogue voltage source and ground to filter out any frequencies too high to be detected by the microcontroller. Here we can see the effects of a high frequency signal the output is aliased and instead of a nice single trace we seem to get multiple traces occurring simultaneously.
Here is a picture of the finished project. As you can see the two analogue traces are being plotted onto the screen, one in red and the other in green. The oscillations seen in the red line are from the LDR sensor picking up 50hz mains interference via the strip lights in the Matrix development office.
To take this project further a trigger value could be used to try to replicate the signal triggering function of an oscilloscope. This would allow for recurring signals to be analysed or for precise events to be captured onto the screen. Another use would be to take the values and output them to a log file allowing for future plotting or long term signal analysis such as sunlight levels measured over several years.
Using an ECIO combined with the Flowcode USB and Analogue components it is possible to create a very low cost basic scope that will replicate most of the functionality from an oscilloscope. As the scope data is also going into a PC it is also possible to take this further. For example you could measure the ambient light level outside and then when it gets dark you could automatically shut your curtains.
The demonstration program uses the Flowcode USB Slave component to transfer the voltage data from the ECIO to the PC. The ECIO device sits in a loop waiting for communications from the PC. At the same time, the analogue channel is being sampled as part of a timer interrupt service routine. Each sample is placed into a data buffer consisting of an array of byte variables. When the request from the PC comes into the ECIO device the data buffer is copied to the outgoing USB buffer and the ECIO then starts again from the beginning of the buffer. It is therefore possible to change the timer interrupt rate and directly control the analogue sample rate.
The PC software was created using the free VB 2008 Express Edition and simply outputs the incoming data by plotting points on an x y graph. Each time the PC requests data from the ECIO it receives the complete buffer and straight away plots the buffer onto the graph. The graph can hold 600 values so the time frame for this is 600 multiplied by the ECIO timer interrupt period. The example comes with a timer interrupt rate of 732.4hz which corresponds to an interrupt period of 1/f = 1.365ms or a sample window of 600 x 0.00136 = 0.82 seconds.
The output buffer for the USB slave component is 32 bytes in length by default. I have used the custom code feature of Flowcode to modify the USB Slave component to allow for a much larger array size of 128 bytes. The USB Slave component uses the Defines section of code to define the USB buffer sizes. I have modified the default settings by finding the line shown below:
#define USB_EP1_IN_SIZE 32
And changing it to this.
#define USB_EP1_IN_SIZE 128
You will also need to rearrange the in and out buffers for endpoint 1 to allow the increased buffer to fit in with the other USB buffers.
Eg
#define USB_EP0_OUT_ADDR 0x0500
#define USB_EP0_IN_ADDR 0x0508
#define USB_EP1_OUT_ADDR 0x0510
#define USB_EP1_IN_ADDR 0x0530
The maximum buffer size that can be used on a PIC is 256 so if you want a really fast scope then you could possibly double the buffer size again to give even more data throughput. This has to be balanced with how fast the VB can collect and process the data so as to avoid gaps in the output signal trace. In an attempt to try and speed up the VB.net code I have used a technique called double buffering where the output data is plotted onto a hidden image. Once the data has been plotted onto the image the image is then loaded onto the screen. This is much faster then trying to plot the signals directly onto the screen.
To provide the analogue sources I have connected an EB003 sensor board to Port A of the ECIO and used the onboard LDR and potentiometer. Alternatively you could connect the analogue inputs to just about anything to read the voltage. You may want to put a 10K series resistor between the analogue input pin and the analogue voltage source to avoid any potential damage to the ECIO microcontroller. Here is a good example circuit that will suit most types of analogue resistive sensor.
It is also good practise to add a ceramic capacitor between the analogue voltage source and ground to filter out any frequencies too high to be detected by the microcontroller. Here we can see the effects of a high frequency signal the output is aliased and instead of a nice single trace we seem to get multiple traces occurring simultaneously.
Here is a picture of the finished project. As you can see the two analogue traces are being plotted onto the screen, one in red and the other in green. The oscillations seen in the red line are from the LDR sensor picking up 50hz mains interference via the strip lights in the Matrix development office.
To take this project further a trigger value could be used to try to replicate the signal triggering function of an oscilloscope. This would allow for recurring signals to be analysed or for precise events to be captured onto the screen. Another use would be to take the values and output them to a log file allowing for future plotting or long term signal analysis such as sunlight levels measured over several years.