write Value

Write a name:value pair and a newline character (\r\n) to the serial port.

serial.writeValue("x", 0)

It is common when reporting or recording data to use a Name Value Pair (NVP). They appear as a text output string in the form of a name and a value together. The name and the value are separated in the string with a colon, :. A name value pair reporting a temperature of -15 degrees could look like:


Associating a name with a value helps to identify related data when different data sources are recorded. For example, if you’re reporting both temperature and light intensity, the name:value format helps spreadsheets or other data analysis programs distinguish between them and group the same types of values together properly. Reporting two data sources might look like this in the output:



  • name is the string to write to the serial port
  • value is the number to write to the serial port

Example: streaming data

Every 10 seconds, the example below sends the temperature and light level to the serial port.

basic.forever(function() {
    serial.writeValue("temp", input.temperature())
    serial.writeValue("light", input.lightLevel())

The send value function broadcasts string/number pairs. You can use a second micro:bit to receive them, and then send them directly to the serial port with write value.

See also

serial, serial write line, serial write number, send value