This activity demonstrates how to design and play a simple wireless musical keyboard using three pushbuttons, a piezospeaker and two RF Transceiver modules. Two HomeWork boards will be used: one outfitted with three pushbuttons and an RF Transceiver module for transmitting data, and the other with a piezospeaker and an RF Transceiver module for receiving data. When complete, you will be able to press buttons on one board and a tone will play through the piezospeaker on the other.
If you are new to the BASIC Stamp microcontroller or to programming, it would be a good idea to review the following before beginning:
(2) HomeWork Boards with BASIC Stamp 2 (#28158)
The BASIC Stamp 2 on a Board of Education (#28103) is also suitable for this activity
(2) 433 MHz RF Transceivers (#27982)
(1) Piezospeaker (#900-00001)
(3) Pushbuttons (#400-00002)
(5) 220 Ω resistors (#150-02210)
(3) 10 kΩ resistors (#150-01030)
Source code for the Wireless Musical Keyboard [1]
Some of the most exciting BASIC Stamp applications can be accomplished with Radio Frequency (RF) transmitters and receivers. The best part is that RF communication is all around! Every time you listen to the radio, use a cell phone, or watch television, RF transceivers are at work. RF communication uses radio waves instead of wires for exchanging signals, which is where the name “wireless communication” comes from. RF modules use frequencies (measured in Hz) to distinguish different radio signals, so in order for RF modules to “talk”, they must be operating on the same frequency. This activity uses the 433 MHz Parallax RF Transceiver modules. The modules have a transmitter that sends signals using radio waves that oscillate at 433 MHz, and a receiver that is tuned to the 433 MHz frequency, so a pair can communicate with each other.
Since RF modules replace the wires that devices would otherwise need to exchange information, let’s first take a look at how BASIC Stamp modules would exchange information if they were connected by wires. BASIC Stamp modules (and a myriad of other digital devices) exchange information using serial communication. For example, a BASIC Stamp sending the character “E” to another BASIC Stamp could use the command SEROUT TxPin, 16470, [“E”]. You can look up the SEROUT command in the BASIC Stamp Manual or the BASIC Stamp Editor’s Help feature. One of the thing’s you’ll find is a table that tells you when the SEROUT command’s baudmode argument is set to 16470, it means that 8-bit data is sent inverted at a baud rate of 2400 bits/second.
Figure 1 shows an example of this exchange. Since the baud rate is 2400 bits/second, each high/low signal has to last for 1/2400 of a second. Inverted means that every binary 1 is transmitted as a 0, and vice-versa, and 8-bit means that each message has a byte’s worth of information (8 bits).
Figure 1 - Serial Transmission Between Two Connected BASIC Stamp Modules
The ASCII value for the character “E” is 69, which is %01000101 in binary. So why does it appear that the data is transmitted as %101011101?
The answer is for two reasons. First, the SEROUT command is sending ‘inverted’ data, and second, serial communication always sends its values least significant bit (LSB) first.
A serial message also has a start and stop bit, shown below in Figure 2. At a baud rate of 2400 bits-per-second (inverted), the start bit is a high signal that lasts 1/2400 of a second, and it signals that 8 more 1/2400 of a second bits are on their way. The stop bit is the minimum time before the transmitter is allowed to send another byte. The BASIC Stamp uses 1 stop bit, which at 2400 bits/second means that the transmitter has to wait at least 1/2400 of a second before sending the next byte.
Figure 2 - How Data is Transmitted Serially
So we’ve covered transmitting serial data when two BASIC Stamp modules are connected. What if we want them to transmit the data wirelessly, like we will in the following activity? Does the theory change completely? Not at all. In fact, you can simply replace the wire with an RF transmitter and receiver, and the two BASIC Stamp modules can still exchange data as though they were using a wire. Figure 3 shows how this works. The only difference is that the transmitter gets data from the BASIC Stamp and sends it out through an antenna and into the air via radio waves, and the receiver module demodulates the radio signals back to wired serial signals and transmits them to the receiving BASIC Stamp.
Figure 3 - How Data is Transmitted Wirelessly
Figure 4 - RF Transmitter Board Schematic
Figure 5 - RF Receiver Board Schematic
Figures 6 & 7 - Circuit wiring for the transmitter (left) and receiver (right) boards
Wiring for the two boards should look something like the above figures.
' {$STAMP BS2} ' {$PBASIC 2.5} ' MultiButtonTx.bs2 ' Transmit pushbutton states for multiple pushbuttons using the 433 MHz ' RF Transceiver. TxPin PIN 14 TR PIN 15 Baud CON 16468 PbStates VAR Byte HIGH TR ' T/R Line High - Transmit DO PbStates = INA ' Input Pins P3..P0 SEROUT TxPin, Baud, [PbStates] ' Transmit all push button states PAUSE 10 LOOP
' {$STAMP BS2} ' {$PBASIC 2.5} ' MultiButtonRx.bs2 ' Receive pushbutton states using the 433 MHz RF Transceiver and play ' tones based on what's received. RxPin PIN 10 TR PIN 11 Baud CON 16468 PbStates VAR Byte LOW TR ' T/R Line low - receive DO SERIN RxPin, Baud, [PbStates] ' Read all Pushbutton states SELECT PbStates ' Select all Pushbutton states CASE %0001 ' Pushbutton at P0 is pressed GOSUB Play_C ' Execute subroutine Play_C CASE %0010 ' Pushbutton at P1 is pressed GOSUB Play_G ' Execute subroutine Play_G CASE %0100 ' Pushbutton at P2 is pressed GOSUB Play_A ' Execute subroutine Play_A ENDSELECT LOOP Play_C: FREQOUT 4, 500, 2093 RETURN Play_G: FREQOUT 4, 500, 3136 RETURN Play_A: FREQOUT 4, 500, 3520 RETURN
The program MultiButtonTx.bs2 utilizes the SEROUT command to transmit the states of the pushbuttons to the receiver board. The INA argument is what’s called the “Nibble Name” which monitors the states of all devices connected to pins 0-3. Recall that the command DEBUG ? IN3 prints “IN3 = 1” or “IN3 = 0” in the Debug Terminal depending on whether the pushbutton is pressed. So when the command INA is called, it’s really the same as calling IN0, IN1, IN2, IN3. Then, when SEROUT TxPin, Baud, [PbStates] is called; it then sends the state of each pushbutton connected to P0, P1, P2, and P3 to the Parallax 433 MHz RF Transceiver.
The program MultiButtonRx.bs2 utilizes the SEROUT command to receive the states of the pushbuttons from the transmitter board. However, it’s also a bit more complicated, since once the states are received, the program then has to decide what to do. In order to sort each pushbutton state, the program uses a SELECT…CASE command. Once SELECT PbStates is called, the received values of each pushbutton state are loaded into RAM as a byte. However, there are four states that are received.
So how does the BASIC Stamp 2 store these values? The simplest way to analyze how this is done is to read each byte in binary. Since PbStates was defined as a Byte, there are four “spots” for the state of each pushbutton. Remember, binary only stores values as 0’s or 1’s, and 0 is usually denoted as low and 1 as high. So if we assign each “spot” to one pushbutton, the CASE command can be used to execute subroutines based on PbStates’s binary value. The table below shows examples of how the BASIC Stamp will read each pushbutton state based on the binary value it has received. Our program is checking the states of the first three pushbuttons, but the INA command can check the first three pins, if desired.
Binary Value |
Pushbutton Reaction |
%0000 |
No PB Pressed |
%0001 |
PB connected to P0 pressed |
%0010 |
PB connected to P1 pressed |
%0100 |
PB connected to P2 pressed |
%0011 |
PB connected to P0 & P1 pressed |
%0111 |
PB connected to P0, P1 & P2 pressed |
Therefore, by knowing the binary equivalent of each pushbutton being pressed; the CASE command can be used to compare each binary condition to PbStates and execute a subroutine based on that value. In this program, depending on which pushbutton is pressed, a different note will play.
Links
[1] https://learn.parallax.com/sites/default/files/Files/Docs/Projects/Wireless-Keyboard/Wireless-Keyboard.zip