# Google Docs Sensor Logging From Your PC

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/086/352/medium800/sensors_banner2.png?1577042838)

This guide will show you how you may log data from an I2C or SPI based sensor to a Google Sheet, directly from your PC, using Python.

**No microcontrollers are required - we read data directly into a computer!**

To enable reading I2C or SPI based sensors from your PC, we'll use a special USB bridge device. There are several options:

### Adafruit MCP2221A Breakout - General Purpose USB to GPIO ADC I2C

[Adafruit MCP2221A Breakout - General Purpose USB to GPIO ADC I2C](https://www.adafruit.com/product/4471)
Wouldn't it be cool to drive a [tiny&nbsp;OLED display](https://www.adafruit.com/categories/98), read a [color...](https://www.adafruit.com/products/1334)

In Stock
[Buy Now](https://www.adafruit.com/product/4471)
[Related Guides to the Product](https://learn.adafruit.com/products/4471/guides)
![Adafruit MCP2221A Breakout - General Purpose USB to GPIO ADC I2C connected to a computer with an IMU sensor that is streaming data to some graphing program](https://cdn-shop.adafruit.com/product-videos/640x480/4471-04.jpg)

### Adafruit FT232H Breakout - General Purpose USB to GPIO, SPI, I2C

[Adafruit FT232H Breakout - General Purpose USB to GPIO, SPI, I2C](https://www.adafruit.com/product/2264)
Wouldn't it be cool to drive a [tiny&nbsp;OLED display](https://www.adafruit.com/categories/98), read a [color...](https://www.adafruit.com/products/1334)

In Stock
[Buy Now](https://www.adafruit.com/product/2264)
[Related Guides to the Product](https://learn.adafruit.com/products/2264/guides)
![Angled Shot of the Adafruit FT232H Breakout - General Purpose USB to GPIO, SPI, I2C](https://cdn-shop.adafruit.com/640x480/2264-07.jpg)

And then we can install [Blinka](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux) to allow us to use [CircuitPython libraries](https://circuitpython.org/libraries) to make using the sensor easy.

There's a fair amount of initial software setup that must be done, most of which is for setting up services and credentials on Google. But once everything is in place, sending sensor data to a Google Sheet is really easy.

Let's get started...

![](https://cdn-learn.adafruit.com/assets/assets/000/086/350/medium800/sensors_banner.jpg?1577041301)

# Google Docs Sensor Logging From Your PC

## Software Setup

There are three main pieces to get this all working:

- **Google Setup -** do this in your Google account to enable features and create credentials you can use to access your Google Docs.
- **Python Setup -** install libraries to allow accessing your docs using credentials created above
- **Sensor Setup** - install necessary software to access I2C/SPI sensors via USB

We'll go through each of these separately in the following three sections.

# Google Docs Sensor Logging From Your PC

## Google Setup

These steps are taken via a web browser while logged in to your Google user account.

This is probably the trickiest part. It's really just a matter of going to your Google account to enable features and create credentials, which you will then use in your Python program to access your Google Docs. But the specific details, like the user interface, menu locations, specific names for things, etc. seem to always be changing. So getting through this part can be a bit of a slog.

But here we go...

## Create a New Project

Follow these steps to create a new project.

- From your [Google API Dashboard](https://console.developers.google.com), click **CREATE**
- **NOTE** : If you have already existing projects, this screen will look different.

![sensors_api_setup_1.png](https://cdn-learn.adafruit.com/assets/assets/000/086/329/medium640/sensors_api_setup_1.png?1577038026)

- Give the project a name, like **Sheets API**
- You can leave **Organization** and **Location** blank or with default values
- Click **CREATE**

![sensors_api_setup_2.png](https://cdn-learn.adafruit.com/assets/assets/000/086/330/medium640/sensors_api_setup_2.png?1577038117)

- You should be returned to the main Dashboard screen and your new project is selected.

![sensors_api_setup_3.png](https://cdn-learn.adafruit.com/assets/assets/000/086/331/medium640/sensors_api_setup_3.png?1577038197)

## Enable APIs for the Project

Follow these steps to add the needed APIs to the Project.

- From the Dashboard screen, click **ENABLE APIS AND SERVICES**

![sensors_api_setup_4.png](https://cdn-learn.adafruit.com/assets/assets/000/086/332/medium640/sensors_api_setup_4.png?1577038248)

- In the Search box, enter "drive"

![sensors_api_setup_5.png](https://cdn-learn.adafruit.com/assets/assets/000/086/333/medium640/sensors_api_setup_5.png?1577038307)

- After entering "drive" in the Search box, select **Google Drive API** from the list.

![sensors_api_setup_6.png](https://cdn-learn.adafruit.com/assets/assets/000/086/334/medium640/sensors_api_setup_6.png?1577038369)

- On the next screen, click **ENABLE**.

![sensors_api_setup_7.png](https://cdn-learn.adafruit.com/assets/assets/000/086/335/medium640/sensors_api_setup_7.png?1577038427)

- Now search for "sheets" and select **Google Sheets API**

![sensors_api_setup_8.png](https://cdn-learn.adafruit.com/assets/assets/000/086/336/medium640/sensors_api_setup_8.png?1577038501)

- On the next screen, click **ENABLE**.

![sensors_api_setup_9.png](https://cdn-learn.adafruit.com/assets/assets/000/086/337/medium640/sensors_api_setup_9.png?1577038575)

- Back at the main Dashboard, verify that the two APIs have been added.

![sensors_api_setup_10.png](https://cdn-learn.adafruit.com/assets/assets/000/086/338/medium640/sensors_api_setup_10.png?1577038738)

## Create a Service Account

Follow these steps to create the credentials needed to access your Sheet from Python.

- Click on the 3 bars to access the menu and go to **IAM & admin -\> Service accounts**

![sensors_api_setup_11.png](https://cdn-learn.adafruit.com/assets/assets/000/086/339/medium640/sensors_api_setup_11.png?1577038910)

- On the Service Accounts screen, click **CREATE SERVICE ACCOUNT**.

![sensors_api_setup_12.png](https://cdn-learn.adafruit.com/assets/assets/000/086/340/medium640/sensors_api_setup_12.png?1577039003)

- Give the service account a name like **google-sheets-api** and click **CREATE**.

![sensors_api_setup_13.png](https://cdn-learn.adafruit.com/assets/assets/000/086/341/medium640/sensors_api_setup_13.png?1577039128)

- Click **CONTINUE** on the next screen.

![sensors_api_setup_14.png](https://cdn-learn.adafruit.com/assets/assets/000/086/342/medium640/sensors_api_setup_14.png?1577039239)

- On the final screen, click **CREATE KEY**.

![sensors_api_setup_15.png](https://cdn-learn.adafruit.com/assets/assets/000/086/343/medium640/sensors_api_setup_15.png?1577039358)

- Make sure **JSON** is selected and click **CREATE**.

![sensors_api_setup_16.png](https://cdn-learn.adafruit.com/assets/assets/000/086/344/medium640/sensors_api_setup_16.png?1577039467)

- You will be prompted to save a **.json** file. Save this somewhere safe.

![sensors_api_setup_17.png](https://cdn-learn.adafruit.com/assets/assets/000/086/345/medium640/sensors_api_setup_17.png?1577039553)

Danger: 

Everything should now be enabled and set up.

**If you have created and saved the .json file successfully, then move on to the next step.**

# Google Docs Sensor Logging From Your PC

## Python Setup

Here we install the required Python libraries and create a test Sheet to make sure everything works. More information can be found in the Google documentation here:

[Google Docs API Python Quickstart](https://developers.google.com/docs/api/quickstart/python)
## Download Python (mainly for Windows Users)

Python 3 comes standard on Linux and macOS. But Windows users likely do not have it installed as a default. If you need Python and the pip installer, go to [https://www.python.org/downloads/](https://www.python.org/downloads/) and download the latest version of Python 3.x for your operating system.

## Install Python Libraries

We need two libraries:

- [google-auth-oauthlib](https://pypi.org/project/google-auth-oauthlib/) - needed for authentication
- [google-api-python-client](https://pypi.org/project/google-api-python-client/) - the main Google API client library

which we can install with pip (which is installed with Python 3). Go to a command prompt and type:

```python
pip3 install google-api-python-client google-auth-oauthlib
```

## Create A Test Sheet

To test everything, let's go ahead and create a Sheet.

Log in to your Google account and go to Docs and create a new Sheet and give it a name like **Test Sheet** :

![](https://cdn-learn.adafruit.com/assets/assets/000/086/251/medium800/sensors_Screenshot_from_2019-12-21_08-38-38.png?1576957743)

Under **File -\> Share** , add the `"client_email"` address from the **.json** file created in the previous step. You can open the **.json** file in a text editor to find this value.

![](https://cdn-learn.adafruit.com/assets/assets/000/086/252/medium800/sensors_Screenshot_from_2019-12-21_08-38-53.png?1576957760)

You'll also need the `spreadsheetID` which you can get from the URL. It is the big long gibberish value between "/d/" and "/edit". ([more info](https://developers.google.com/sheets/api/guides/concepts#spreadsheet_id))

## Run Example

Then, try running this example:

Info: 

```python
from google.oauth2.service_account import Credentials
from googleapiclient.discovery import build

SERVICE_ACCOUNT_FILE = 'YOUR_CREDENTIALS_FILE.json'
SPREADSHEET_ID = 'YOUR_SPREADSHEET_ID'
SCOPES = ['https://spreadsheets.google.com/feeds',
          'https://www.googleapis.com/auth/drive']

credentials = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)

service = build('sheets', 'v4', credentials=credentials)

sheet = service.spreadsheets()

range = 'A1'
values = [ ['test'], ]
body = {'values' : values}

sheet.values().append(spreadsheetId=SPREADSHEET_ID, 
                      valueInputOption='RAW',
                      range=range,
                      body=body).execute()
```

Replace `YOUR_CREDENTIALS_FILE` with the actual name of your **.json** file and `YOUR_SPREADSHEET_ID` with what you got from the URL. Save the above as something like **gspread\_test.py** and then try running it:

```python
python3 gspread_test.py
```

It should run without any errors. If you then go look at your spreadsheet, you should see **test** in cell **A1** , like this:

![](https://cdn-learn.adafruit.com/assets/assets/000/086/253/medium800/sensors_Screenshot_from_2019-12-21_08-49-16.png?1576958151)

If any errors do occur, try and troubleshoot them. Hopefully they will contain helpful messages.

**If the above test worked, move on to the next step.**

# Google Docs Sensor Logging From Your PC

## Sensor Setup

To get sensor data into your PC, we will use a special USB gadget to provide the necessary hardware interface. There are several options available. Go to the appropriate guide below for the device you are using.

[MCP2221A (I2C)](https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221)
[FT232H (SPI or I2C)](https://learn.adafruit.com/circuitpython-on-any-computer-with-ft232h)
[Binho Nova (SPI or I2C)](https://learn.adafruit.com/circuitpython-with-binho-nova-multi-protocol-usb-host-adapter)
You will end up installing [Blinka](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/overview), which the guides cover. Each of those guides also has a simple test to make sure you can talk to a sensor. You can also look at the guide specific to your sensor for additional help with wiring and additional code examples.

**Once you are talking to your sensor, move on to the next step.**

# Google Docs Sensor Logging From Your PC

## Sensor Logging

OK, let's put all the pieces together and log some sensor data. At this point you should have:

- Created a Google project and enabled Drive and Sheets API
- Created a .json credentials file and saved it locally
- Created a Google Sheet and shared it with the email address from the .json file
- Installed the Python libraries `google-api-python-client` and `google-auth-oauthlib`
- Installed Blinka to allow using CircuitPython libraries
- Installed the CircuitPython library for your specific sensor

The following examples show usage for two different USB bridge options as well as for I2C and SPI.

# Google Docs Sensor Logging From Your PC

## MCP2221 with BME280 on I2C

This example uses a MCP2221 with a BME280 pressure/temperature/humidity sensor accessed over an I2C wired connection. Links to setup the MCP2221 were provided early in the guide. To install the library for the BME280, see here:

[Python Installation of BME280 Library](https://learn.adafruit.com/adafruit-bme280-humidity-barometric-pressure-temperature-sensor-breakout/python-circuitpython-test#python-installation-of-bme280-library-5-10)
Here is the wiring diagram:

![](https://cdn-learn.adafruit.com/assets/assets/000/086/302/medium800/sensors_mcp2221_bme280_bb.png?1576976937)

Create a new Sheet to hold this data and give it a name. Here we also add some column titles:

![](https://cdn-learn.adafruit.com/assets/assets/000/086/303/medium800/sensors_BME280_example_1.png?1576977613)

Add just like we did in the test example, share the sheet with the `"client email"` address from the **.json** file. Click the **SHARE** button:

![](https://cdn-learn.adafruit.com/assets/assets/000/086/354/medium800/sensors_sheet_share1.png?1577043364)

And add the `"client email"` address from the **.json** file.

![](https://cdn-learn.adafruit.com/assets/assets/000/086/355/medium800/sensors_sheet_share2.png?1577043378)

Also, grab the gibberish `spreadsheetID` from the URL so you can use it in the code below. See previous section of guide for more info on this.

![](https://cdn-learn.adafruit.com/assets/assets/000/086/356/medium800/sensors_sheet_share3.png?1577043476)

And here's the code for logging BME280 sensor data to you Google Sheet. **Be sure to replace `YOUR_CREDENTIALS_FILE` and `YOUR_SHEET_ID` with the actual values for your setup.**

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Google_Sheet_Sensor_Logging/bme280_logger.py

Danger: 

Save that as something like **bme280\_logger.py** and then run it with:

```python
python3 bme280_logger.py
```

It should run without errors and start logging:

![](https://cdn-learn.adafruit.com/assets/assets/000/086/305/medium800/sensors_BME280_example_0.png?1576977941)

After a couple of minutes, go back and look at your sheet. It should be updated with new values:

![](https://cdn-learn.adafruit.com/assets/assets/000/086/304/medium800/sensors_BME280_example_2.png?1576977739)

# Google Docs Sensor Logging From Your PC

## FT232H with MAX31855 on SPI

This example uses a FT232H with a MAX31855 thermocouple breakout accessed over SPI. Links to setup the FT232H were provided early in the guide. To install the library for the MAX31855, see here:

[Python Installation of MAX31855 Library](https://learn.adafruit.com/thermocouple/python-circuitpython#python-installation-of-max31855-library-4-6)
Here is the wiring diagram:

![](https://cdn-learn.adafruit.com/assets/assets/000/086/950/medium800/sensors_ft232h_max31855_bb.png?1579201561)

## Sensor

You will also need an appropriate thermocouple sensor attached to the MAX31855 **Red -** and **Yellow +** terminals. See the [Adafruit thermocouple guide](https://learn.adafruit.com/thermocouple) in selecting the appropriate sensor.

Create a new Sheet to hold this data and give it a name. Here we also add some column titles:

![](https://cdn-learn.adafruit.com/assets/assets/000/086/951/medium800/sensors_max_sheet_1.png?1579201574)

Add just like we did in the test example, share the sheet with the `"client email"` address from the **.json** file. Click the **SHARE** button:

![](https://cdn-learn.adafruit.com/assets/assets/000/086/952/medium800/sensors_max_sheet_2.png?1579201590)

And add the `"client email"` address from the **.json** file.

![](https://cdn-learn.adafruit.com/assets/assets/000/086/953/medium800/sensors_max_sheet_3.png?1579201603)

Also, grab the gibberish `spreadsheetID` from the URL so you can use it in the code below. See previous section of guide for more info on this.

![](https://cdn-learn.adafruit.com/assets/assets/000/086/954/medium800/sensors_max_sheet_4.png?1579201615)

And here's the code for logging MAX31855 sensor data to you Google Sheet. **Be sure to replace `YOUR_CREDENTIALS_FILE` and `YOUR_SHEET_ID` with the actual values for your setup.**

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Google_Sheet_Sensor_Logging/max31855_logger.py

Danger: 

Save that as something like **max31855\_logger.py** and then run it with:

```python
python3 max31855_logger.py
```

It should run without errors and start logging:

![](https://cdn-learn.adafruit.com/assets/assets/000/086/955/medium800/sensors_max_terminal.png?1579201625)

After a couple of minutes, go back and look at your sheet. It should be updated with new values:

![](https://cdn-learn.adafruit.com/assets/assets/000/086/956/medium800/sensors_max_sheet_5.png?1579201634)


## Featured Products

### Adafruit MCP2221A Breakout - General Purpose USB to GPIO ADC I2C

[Adafruit MCP2221A Breakout - General Purpose USB to GPIO ADC I2C](https://www.adafruit.com/product/4471)
Wouldn't it be cool to drive a [tiny&nbsp;OLED display](https://www.adafruit.com/categories/98), read a [color...](https://www.adafruit.com/products/1334)

In Stock
[Buy Now](https://www.adafruit.com/product/4471)
[Related Guides to the Product](https://learn.adafruit.com/products/4471/guides)
### Adafruit FT232H Breakout - General Purpose USB to GPIO, SPI, I2C

[Adafruit FT232H Breakout - General Purpose USB to GPIO, SPI, I2C](https://www.adafruit.com/product/2264)
Wouldn't it be cool to drive a [tiny&nbsp;OLED display](https://www.adafruit.com/categories/98), read a [color...](https://www.adafruit.com/products/1334)

In Stock
[Buy Now](https://www.adafruit.com/product/2264)
[Related Guides to the Product](https://learn.adafruit.com/products/2264/guides)
### Binho Nova Multi-Protocol USB Host Adapter

[Binho Nova Multi-Protocol USB Host Adapter](https://www.adafruit.com/product/4459)
[Discontinued -](https://www.adafruit.com/product/5056)[**you can grab our&nbsp;**](https://www.adafruit.com/product/5056)Adafruit Trinkey QT2040 - RP2040 USB Key with Stemma QT **&nbsp;instead!**

The&nbsp; **Binho...**

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/4459)
[Related Guides to the Product](https://learn.adafruit.com/products/4459/guides)
### Adafruit BME280 I2C or SPI Temperature Humidity Pressure Sensor

[Adafruit BME280 I2C or SPI Temperature Humidity Pressure Sensor](https://www.adafruit.com/product/2652)
Bosch has stepped up their game with their new BME280 sensor, an environmental sensor with temperature, barometric pressure&nbsp;and&nbsp;humidity! This sensor is great for all sorts of indoor environmental sensing and can even be used in both I2C and SPI!

This precision sensor from...

Out of Stock
[Buy Now](https://www.adafruit.com/product/2652)
[Related Guides to the Product](https://learn.adafruit.com/products/2652/guides)
### Thermocouple Amplifier MAX31855 breakout board (MAX6675 upgrade)

[Thermocouple Amplifier MAX31855 breakout board (MAX6675 upgrade)](https://www.adafruit.com/product/269)
Thermocouples are very sensitive, requiring a good amplifier with a cold-compensation reference. The MAX31855K does everything for you, and can be easily interfaced with any microcontroller, even one without an analog input. This breakout board has the chip itself, a 3.3V regulator with 10uF...

In Stock
[Buy Now](https://www.adafruit.com/product/269)
[Related Guides to the Product](https://learn.adafruit.com/products/269/guides)
### Half Size Breadboard + 78 Piece 22AWG Jumper Wire Bundle

[Half Size Breadboard + 78 Piece 22AWG Jumper Wire Bundle](https://www.adafruit.com/product/3314)
This is a cute half-size breadboard with an assortment of small jumper wires, great for prototyping. The breadboard is 2.2" x 3.4" (5.5 cm x 8.5 cm) with a standard double-strip in the middle and two power rails on both sides. Power rails can be pulled off easily to make the...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3314)
[Related Guides to the Product](https://learn.adafruit.com/products/3314/guides)

## Related Guides

- [CircuitPython Libraries on any Computer with MCP2221](https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221.md)
- [CircuitPython Libraries on Linux and ODROID C2](https://learn.adafruit.com/circuitpython-libaries-linux-odroid-c2.md)
- [CircuitPython NeoPixel Library Using SPI](https://learn.adafruit.com/circuitpython-neopixels-using-spi.md)
- [I2C Addresses and Troublesome Chips](https://learn.adafruit.com/i2c-addresses.md)
- [Programming Microcontrollers using OpenOCD on a Raspberry Pi](https://learn.adafruit.com/programming-microcontrollers-using-openocd-on-raspberry-pi.md)
- [No-Code Offline Data Logger with WipperSnapper](https://learn.adafruit.com/no-code-offline-data-logging-with-wippersnapper.md)
- [Adafruit 1-Wire Thermocouple Amplifier - MAX31850K](https://learn.adafruit.com/adafruit-1-wire-thermocouple-amplifier-max31850k.md)
- [CircuitPython Libraries with the Binho Nova Multi-Protocol USB Host Adapter](https://learn.adafruit.com/circuitpython-with-binho-nova-multi-protocol-usb-host-adapter.md)
- [DIY Trinkey No-Soldering USB Air Quality Monitor](https://learn.adafruit.com/diy-trinkey-no-solder-air-quality-monitor.md)
- [CircuitPython Libraries and Jupyter Notebook on any Computer with MCP2221](https://learn.adafruit.com/jupyter-on-any-computer-with-circuitpython-libraries-and-mcp2221.md)
- [MAX31855 Thermocouple Sensor Python Library](https://learn.adafruit.com/max31855-thermocouple-python-library.md)
- [Air Quality Sensor 3D Printed Enclosure](https://learn.adafruit.com/air-quality-sensor-silo-house.md)
- [Using Piezo Buzzers with WipperSnapper](https://learn.adafruit.com/using-piezo-buzzers-with-wippersnapper.md)
- [CircuitPython Libraries on any Computer with FT232H](https://learn.adafruit.com/circuitpython-on-any-computer-with-ft232h.md)
- [CircuitPython I2C and SPI Under the Hood](https://learn.adafruit.com/circuitpython-basics-i2c-and-spi.md)
- [Programming SPI flash with an FT232H breakout](https://learn.adafruit.com/programming-spi-flash-prom-with-an-ft232h-breakout.md)
