Micro:bit - Morse Transmitter
Written by Harry Fairhead   
Monday, 01 March 2021
Article Index
Micro:bit - Morse Transmitter
Polling v Events
A Morse Transmitter

A Simple Radio Link

In the spirit of starting with something simple at both ends of the connection, what we need is something that has minimal implementation. The first thing you have to do is enable the raw radio mode by disabling the BLE mode. For both the V1 and V2 you do this in the same way. In each case you need to find the MicroBitConfig.h file and add the line:

#define MICROBIT_BLE_ENABLED 0 

In the case of the micro:bit V2 this is the default state, but it is worth making the change to make sure. When you next compile the project a complete rebuild will occur.

The simplest pair of programs you can use to check that the radio is working are:

Transmitter:

#include "MicroBit.h"
MicroBit uBit;
int main()
{
    uBit.init();
    uBit.radio.enable();
    uBit.display.print("TX");
    while (1)
    {
        if (uBit.buttonA.isPressed())
        {
            uBit.display.print("1");
            uBit.radio.datagram.send("1");
        }
        uBit.sleep(100);
        uBit.display.clear();
    }
    return 0;
}

Receiver:

#include "MicroBit.h"
MicroBit uBit;
void onData(MicroBitEvent e)
{
    ManagedString s = uBit.radio.datagram.recv();
    uBit.display.print(s);
}
int main()
{    uBit.init();
    uBit.messageBus.listen(MICROBIT_ID_RADIO,
                  MICROBIT_RADIO_EVT_DATAGRAM, onData);
    uBit.radio.enable();
    uBit.display.print("RX");
    while (1)
    {
        uBit.sleep(100);
        uBit.display.clear();
    }
}

If you try this out you should see 1 displayed on both micro:bits when Button A is pressed.

This may be a simple example, but it is a good starting point for more complex programs. As a more complicated example of using the radio, our final project is to implement a Morse code system using two micro:bits. To do this it would be nice to have some sound and hence a detour into how to make sound on V1 and V2 is justified.

Not included in this extract:

  • Producing Sounds

Morse Code Transmitter

As a slightly more sophisticated example of using the radio and sound, let’s implement a Morse code transmitter. Morse code is old-fashioned but still relevant and it is worth knowing. The idea is that each letter of the alphabet is coded as a sequence of short and long tones usually called a dot and a dash, for example, the letter A is dot dash. The key idea is that there is no set length of time for a dot or a dash except for the fact that a dash should be three times longer than a dot and letters are separated by a dot length of silence.

Creating a Morse code transmission program is just a matter of setting an LED on and starting a sound when Button A is pressed and turning the LED off and stopping the sound when it is released. The simplest way to do this is to make use of the button down and up events to call appropriate functions. This makes the program work on a local micro:bit. If we use the radio to broadcast the events then the exact same program will signal the code being typed to a remote micro:bit running the exact same program:

#include "MicroBit.h"
MicroBit uBit;
void beepOn(MicroBitEvent e)
{
    uBit.io.P0.setAnalogValue(512);
    uBit.io.P0.setAnalogPeriodUs(1000000 / 500);
    uBit.display.image.setPixelValue(2, 2, 255);
}
void beepOff(MicroBitEvent e)
{
    uBit.io.P0.setAnalogValue(0);
    uBit.display.image.setPixelValue(2, 2, 0);
}
int main()
{
    uBit.init();
    uBit.display.setDisplayMode(DISPLAY_MODE_GREYSCALE);
    uBit.radio.enable();
    uBit.radio.event.listen(MICROBIT_ID_BUTTON_A,
                            MICROBIT_BUTTON_EVT_DOWN);
    uBit.radio.event.listen(MICROBIT_ID_BUTTON_A,
                             MICROBIT_BUTTON_EVT_UP);
    uBit.messageBus.listen(MICROBIT_ID_BUTTON_A,
                      MICROBIT_BUTTON_EVT_DOWN, beepOn);
    uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, 
                       MICROBIT_BUTTON_EVT_UP, beepOff);
    while (1)
    {
        uBit.sleep(100);
    }
}

If you try this out you will need to change P0 to speaker to run on a V2 and make sound on the built-in speaker. You might be surprised that you can redirect the button up and down events on both devices – surely this would create a feedback loop? The reason it doesn’t is that each micro:bit forwards by radio only the presses on its buttons. This makes the program a two-way Morse communication device.

Summary

 

  • Working with radio, or any communications channel, is more difficult because you have two applications to debug and finding out which is causing the problem can be difficult.

  • The best approach is to start from a simple working connection and change small things at each end until you have a finished working project.

  • The micro:bit radio can be used to set up groups of communicating devices by specifying a working frequency and a group number.

  • You can receive data by polling or using events.

  • You can also opt to transfer events that occur on one micro:bit to another micro:bit via the radio link.

  • The micro:bit V1 isn’t powerful enough to create complex sounds, but it can create a beep at a specified frequency using PWM.

  • The micro:bit V2 is powerful enough to generate complex sounds and has a software synthesizer that is based on PWM. Creating sounds with the synthesizer is a matter of defining sound effects using a complex numerical specification.

  • You can use the sound and radio to create a simple two-way Morse code communications device.

 

Micro:bit IoT In C Second Edition

By Harry Fairhead

microbite2360

Buy from Amazon.

Contents

  1. The Micro:bit Family
  2. Getting Started With C/C++ ***NEW!
  3. First Steps With The GPIO
  4. Fast Memory-Mapped GPIO
  5. Pulse Width Modulation, Servos and More
    Extract: Micro:bit - Basic PWM
  6. I2C Bus
  7. I2C Measuring Temperature And Humidity
  8. Creating A Custom Protocol
  9. DS18B20 1-Wire Temperature Sensor
  10. SPI Bus
  11. A‑to‑D With The SPI Bus
  12. Serial Connections
  13. Getting On WiFi
  14. The LED Display
  15. Radio Sounds
    Extract: Morse Transmitter
  16. Appendix I The Mbed Online Compiler
  17. Appendix II Yotta And Offline Development

 <ASIN:1871962676>

<ASIN:B08XPRMK97>

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Rust 1.82 Improves Apple Support
24/10/2024

Following Rust's six-week release cycle, version 1.82 has been released with higher level support for Apple, and a new Info subcommand for Cargo.



Apache Releases Tomcat 11
07/11/2024

Apache has announced the release of Tomcat 11, as well as marking the 25th anniversary of the first commit to the Apache Tomcat source code repository since becoming an ASF project.


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info

<ASIN:B08WTBNJ5Q>

<ASIN:B08NLX7773>

 



Last Updated ( Wednesday, 03 March 2021 )