---
title: Comm
description: Communication
published: 1
date: 2022-06-25T20:12:47.913Z
tags: 
editor: markdown
dateCreated: 2020-12-17T18:57:21.341Z
---

# Comm

| ![][menu] | [![][b1]][d1]&nbsp;&nbsp;&nbsp; [![][b2]][d2]&nbsp;&nbsp;&nbsp; [![][b3]][d3] <br> [![][b4]][d4]&nbsp;&nbsp;&nbsp; [![][b5]][d5]&nbsp;&nbsp;&nbsp; [![][b6]][d6]<br> [![][b7]][d7]&nbsp;&nbsp;&nbsp; [![][b8]][d8]&nbsp;&nbsp;&nbsp; [![][b9]][d9]&nbsp;&nbsp;&nbsp; [![][b10]][d10]<br> [![][b11]][d11]|
|---|:---|

<!--- (COMMENT - INPUT Category Images (Used above)
-->
[spacer]: /reference_manual/comm/spacer.png
[menu]: /reference_manual/menus/menu_comm_70p.png
[b1]: /reference_manual/comm/i2cgetreg_50p.png
[b2]: /reference_manual/comm/i2csetreg_50p.png
[b3]: /reference_manual/comm/spisend_50p.png
[b4]: /reference_manual/comm/spirecv_50p.png
[b5]: /reference_manual/comm/i2cread_50p.png
[b6]: /reference_manual/comm/i2cwrite_50p.png
[b7]: /reference_manual/comm/serialopen_50p.png
[b8]: /reference_manual/comm/serialclose_50p.png
[b9]: /reference_manual/comm/serialread_50p.png
[b10]: /reference_manual/comm/serialwrite_50p.png
[b11]: /reference_manual/comm/serialwritereporter_50p.png

<!--- (COMMENT - INPUT Category Detail Bookmarks (Used above)
-->
[d1]: /reference_manual/comm#i2c-get-register
[d2]: /reference_manual/comm#i2c-set-register
[d3]: /reference_manual/comm#spi-send
[d4]: /reference_manual/comm#spi-receive
[d5]: /reference_manual/comm#i2c-read-list
[d6]: /reference_manual/comm#i2c-write-list
[d7]: /reference_manual/comm#serial-open
[d8]: /reference_manual/comm#serial-close
[d9]: /reference_manual/comm#serial-read
[d10]: /reference_manual/comm#serial-write
[d11]: /reference_manual/comm#serial-write


## <br> Advanced
The Comm category contains blocks that communicate with hardware devices over an Inter-Integrated Circuit (I2C, pronounced "eye squared sea") or Serial Peripheral Interface (SPI) bus. These advanced blocks are usually used by people with hardware experience to create libraries for devices such as sensors or the Hummingbird:bit robotics kit.

To use these blocks, you'll need to study the data sheet for the hardware device of interest. Many I2C and SPI devices must be configured or enabled before use. It is often helpful to find and study the source code of an existing library for the device.

## i2c get register

![](/reference_manual/comm/i2cgetreg.png)

Return the value of the given register (0-255) of the given I2C device (0-127). Check the data sheet for the I2C device for the device address and register assignments.

If successful, this block returns a positive 8-bit value (0-255). A negative return value indicates an error such as the I2C not responding.

## i2c set register

![](/reference_manual/comm/i2csetreg.png)

Writes an 8-bit byte (0-255) to the given I2C device and register.

## spi send

![](/reference_manual/comm/spisend.png)

Sends a byte (0-255) to the i2c device.

SPI hardware transmits and receives data simultaneously. On each clock pulse, a bit is read from the SPI input line while a bit is output on the SPI output line. This block discards the byte received from the SPI bus as the byte is sent.

## spi receive

![](/reference_manual/comm/spirecv.png)

Reads a byte from the SPI device while sending a zero byte and returns the received byte.

## i2c read list

![](/reference_manual/comm/i2cread.png)

Receives multiple bytes from the given I2C device. The caller provides a list whose length is the number of bytes to be read. If successful, this operation replaces the items of that list with the bytes read from the device. 

## i2c write list

![](/reference_manual/comm/i2cwrite.png)

Sends multiple bytes to the I2C device specified. The list should contain integers in the the range 0-255.

## serial open
## Tab {.tabset}
### Description
![](/reference_manual/comm/serialopen.png)

Opens the Serial port at the specified baud rate. This necessary before one starts using the serial port communication.

Depending on the micro device used, serial ports are located on different pins of the device. Please refer to the WIKI [Special Pins](/special_pins) section for the device specific pin number information.

> Serial Port support is NOT available for micro:bit v1 devices due to lacking hardware support. 
{.is-warning}

### Example

| <video controls loop height="300" > <source src="/reference_manual/comm/serialtest.mp4" type="video/mp4"> <p>Your browser cannot play the provided video file.</p> </video> <p style="font-size:6px">TinkerCad Video</p> | ![](/reference_manual/comm/codeserialtest.png =x400) |
|---|---|

This example enables you to experience the Serial Port block set in the easiest way possible. The sample program uses all of the Serial Port blocks.

Simply connect pins 0 and 1 of your micro:bit v2 device with an alligator cable, creating a loopback circuit.

This will make the sample program transmit the string "HELLO" on pin 1 and receive it on pin 0 continuosly, when button-A is pressed.

When you are done, you can press button-B and close the Serial Port.

> **NOTE:** The 500ms delay inserted into the read/display loop of the sample code is only there to make the displayed letters all visible. 
The **read serial** block will function without any delays.
{.is-info}


[Download Example](/reference_manual/examples/code_serialtest.ubp)



## serial close

![](/reference_manual/comm/serialclose.png)

Closes and terminates the serial port communications.

## serial read

![](/reference_manual/comm/serialread.png)

Returns a byte array of data read from the serial port. In cases where these might represent character values, they need to be converted using the **string from unicode** block into actual letters.

![](/reference_manual/comm/bytearrayvalue.png)

Below is an example of string "HELLO" read extracted from the sample program above. What you see is the decimal character codes of the letters:
	H: 72
  E: 69
  L: 76
  L: 76
  O: 79

![](/reference_manual/comm/serialreadbytearray.png)

> Byte Arrays contain byte values of 0 - 255.
{.is-info}


## serial write

![](/reference_manual/comm/serialwrite.png)

Writes a byte, a string, or a byte array to the serial port. The data to be written must be 128 bytes are less. Larger amounts of data can be written with the **serial write** reporter block.

In case you would want to write a byte array to the serial port, you can easily create one with the newly added block [**as byte array**](/reference_manual/data#as-byte-array) under the Data category of blocks.

Here is a picture of what that block outputs:

![](/reference_manual/comm/asbytearrayresult.png)

## serial write starting at

![serialwritereporter.png](/reference_manual/comm/serialwritereporter.png)

Write a byte array to the serial port starting at the given byte offset and return the number of bytes written. Using this block, a MicroBlocks loop can efficiently write a long byte array to the serial port.

![serial_write_loop.png](/reference_manual/comm/serial_write_loop.png)!
