redirect
Configure the serial port to use the pins instead of USB.
serial.redirect(SerialPin.P0, SerialPin.P0, BaudRate.BaudRate115200)
The default connection for the serial port is over a USB cable. You can have the serial data go across wires connected to pins on the micro:bit instead. To set the input and output for the serial connection to be on the pins, you redirect it to the pins. Also, you decide how fast you want to send and receive the data on the pins by choosing a baud rate.
Parameters
- tx: the transmit pin to send serial data on.
- rx: the receive pin to receive serial data on.
- rate: the baud rate for transmitting and receiving data. Baud rates you can choose from are:
300
,1200
,2400
,4800
,9600
,14400
,19200,
,28800
,31250
,38400
,57600
, or115200
Example
Change where serial data is sent to and received from. When button A is pressed, reconfigure the serial port to use the pins. The new configuration uses pin P1
to transmit and P2
to receive. The baud rate is set to 9600
.
input.onButtonPressed(Button.A, function() { serial.redirect(SerialPin.P1, SerialPin.P2, BaudRate.BaudRate9600) })