This Instructable shows the development of a shield for Arduino that can interact with Scratch and replicates the functions of the “Scratch Sensor Board”
Note: The present shield constitutes a so called 'derivative' from "Scratch Sensor Board", whose license can be found here. Further information about the original board can be found here
For the Impatient:
Final Arduino Sketch is here
Printed Circuito Board is here (eagle)
Schematics are here (pdf / eagle)
Step 1: Preparing Ground
After some time reading we found that the Sensor Board communication occurs via RS-232 at a speed of 38400 baud. It is good, because Arduino already have a Serial Port which means less circuits to implement.
The protocol between Scratch and Sensor Board constitutes of a ping pong mechanism. First Scratch application send a a data byte to the board then the board replies with sensor data. Nice.
The request packet is simply a byte with the value 0x01, whilst the sensor data consists of 9 high byte / low byte pairs (i.e. 18 bytes) spaced in time by a 400us interval.
The high byte / low byte pairs contains a 4 bit channel ID and a 10 bit Value. This is good, because if suits well with Arduino ADCs resolution.
For the Scratch Board Release 1 firmware and Scratch release 1.1 and later, the mapping between channel ID and sensor type is given below:
Channel / Sensor
0 / Resistance D
1 / Resistance C
2 / Resistance B
3 / button
4 / Resistance A
5 / Light
6 / Sound
7 / Slider
8-14 / Not Used
15 / Firmware ID (0x04)
From the 16 possible channels we have 7 analog, 1 discrete (on/off), 1 dummy (firmware-id) and 7 spare (not used now; maybe in a future version).
Step 2: The Challenges
If you know Arduino well and paid attention to the last step, you might be noticed that Scratch expects to receive 7 analog readings, but a Stardard Arduino has only 6 channels. That's the first challenge.
To accomplish that, the solution found was to implement a multiplex using a 4066 that is a quad bidirectional CMOS analog switch. One at a time each switch is activated and the correspondent ADC channel is read.
The channels choose to be multiplexed were from the 'light' and 'sound' sensors, since they have stabilizing (filter) capacitors, which make changes in such channels to occur much slower than the switching.
Of course, the switching scheme had to be tested before going on, but luckily, it worked very well!!
A second challenge was to make this shield DIYable. It implies on a Single-Face board with as few jumpers as possible.
To achieve the single face layout I have started by choosing the place of the connectors and the light and sound sensors. Then the association of the Arduino Pins with sensors, as well as the halves of the OPAMP halves and gates of the Analog Switches were experimented until the 'ratsnet' lines showed up aa bit clean. At the end the result was achieved with only 2 jumpers.
(Note: The board available on the files has been further optimized and now only one jumper exists)
To save space and simplify the layout 2 stereo jacks have been used instead of 4 from the original design.
Printed Circuit Board is here (eagle)
Schematics are here (pdf / eagle)
Step 3: Board Construction
The board was etched using Ferric Chloride in a water bath to accelerate the reaction. Despite the potential of the Ferric Chloride to make a mess, I prefer to use it because the etching chemical reaction does not release gases. After corrosion the board was silver coated using TP-065E solution and then it was drilled.
Then the components have been soldered starting by the resistors, then the capacitors, followed by the IC Sockets, After that the taller components have been soldered.
Note: Due to a lack in local stock, the prototype received a different potentiometer.
Using a multimeter all the connections have been tested. I always do that before powering things up because It saves us from a lot of trouble. For example I have found a short circuit caused by a track that was too close from the pads of two resistors, but it wasn't noticed on PCB drawing tool.
(Note: The board available on the files already have this correction)
Step 4: Testing the Board
This practice is called 'validation' and its objective is to reduce the amount of uncertainties and/or errors thus avoid having several problems at the same time to solve.
The first tests have been performed on the Resistance Channels using the 'AnalogInOutSerial' example form Arduino IDE, where the line below was changed according the desired channel.
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
The resistance channels A, B, C, D are mapped to Arduino Analog inputs A0, A1, A2, A3
A cable with a couple of 100K potentiometers and a P2 Plug have been built to help performing such test.
With 100K potentiometers the value shown on serial terminal shall vary between 0 and 930. Use the Serial Monitor from Arduino IDE, like in the example (Tools->Serial Monitor).
The next test was the slider. Still using the 'AnalogInOutSerial' example, change the value of analogInPin to A5
const int analogInPin = A5; // Analog input pin that the potentiometer is attached to
Since the Slider does not have a series resistor, the value shown on serial terminal shall vary from 0 to 1023.
The next step is to perform the test on light and sound sensors, and first we are going to do this without involving the analog multiplexer. Then take out the 4066 and short circuit the pins 3 and 4 from the socket (remember to power off the Arduino before remove the IC).
Yet using the 'AnalogInOutSerial' change the value of analogInPin to A4.
const int analogInPin = A4; // Analog input pin that the potentiometer is attached to
Now, point a flashlight to the sensor and check the values changing on serial terminal. The exact amount of change depends upon the sensitivity of the photo-transistor used.
Now remove the short circuit from pins 3 and 4 and make a short between pins 8 and 9.
Now speak close to the microphone and check that values printed om serial terminal (no need to change the value of analogInPin, since light and sound share the same ADC channel, remember?)
Now we are going to test the multiplex. Remove the short circuit between pins 8 and 9 and reinstall the 4066 (remember to power off the Arduino before inserting the IC).
Use this sketch to test the multiplex. Point a flashlight to the light sensor and speak close to the microphone while watching the values on the serial terminal. You should see both values changing accordingly.
The last tests are the Button. As a starting point use the “Button” example from Arduino and change and check that the LED of the Arduino Board (connected to pin 13) shall go off when the button is pressed and shall be lit when the button is released.
Now, use this sketch to perform an overall test of the board. It reads all the sensors and print the values on the serial terminal. Notice that up to now we have been using the standard 9600 bauds speed from the example sketches, but for the overall tests the speed must be changed to 38400.
Step 5: Testing With Scratch
This final sketch was built upon the "Serial Event" example from Arduino because it fits well with the ping pong (polling) mechanism of the Scratch application.
Then Run Scratch application and then click on the 'sensors' tab at the upleft corner.
Then, at the bottom left there shall be a control named “Sensor”. Right click on it and choose Select "show Watcher")
At the up right corner of the screen, at the left of the yellow cat, a box with the sensor values shall appear.
Right click on it and select the option "select Serial/USB Port"
A list of the available serial ports shall appear. Chose the serial port where the Arduino Board is connected (you can see the port in the Device Manager of Windows).
Now the sensor values shall be appearing. Play with them to check that everything is ok
Now you can use the sensor values in the box and it is possible to use them for interacting with scratch.
Now, use your new shield together with you imagination and... Scratch On!!