Category:PCB Assembly Raspberry Pi

Group Administrators: 1 | Group Member: 47 | Group Threads: 44

The Raspberry Pi is a series of small single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and in developing countries.

Thread Reply
Interface a Raspberry Pi to an I2C temperature sensor Reply 2017-01-22 19:10:43
1K+ views
1 comments
478 likes

Nicolas Giussani

Leave A Message

Follow

This project demonstrates interfacing a Raspberry Pi to an I2C temperature sensor (TMP102) and then publishing the data to a Google sheet and graph.
Requirements
  • Raspberry Pi
    • Used in article: Model B Revision 1.0 with Raspbian (Debian GNU/Linux 7.6 (wheezy))
  • I2C temperature sensor
    • Used in article: TMP102
  • Method to connect Raspberry Pi to internet
    • Use in article: Raspberry Pi wired directly to router
  • Google account to create and access sheets
Setting up the I2CInstalling drivers
  1. Open a terminal on the Pi or use SSH
  2. Type the following on the terminal to install support tools:
sudo apt-get install python-smbus
sudo apt-get install i2c-tools
  • Use raspi-config to enable the i2c drivers:
    sudo raspi-config
    1. Select "Advanced Options"
    2. Select "I2C"
    3. Select "Yes"
    4. Select "Yes"
  • Reboot the Pi
  • Connecting the wires
    Raspberry Pi TMP102 Board
    3v3 Power VCC
    Ground GND
    SDA SDA
    SCL SCL
    Ground ADD0
    Testing the connection
    1. Open a terminal on the Pi or use SSH
    2. Type the following:
    sudo i2cdetect -y 0
    1. Note, if you get an error message "Error: Could not open file `/dev/i2c-0' or `/dev/i2c/0': No such file or directory", follow these steps:
      1. Add the following lines to /etc/modules:
    i2c_bcm2708
    i2c_dev
  • Reboot the pi
  • Alternatively load the modules at runtime by typing the following:
  • sudo modprobe i2c_bcm2708
    sudo modprobe i2c_dev
  • You should see the temp sensor at address 0x48.
  • Run the following python script and verify the temperature reported is the same as in the room using the command:
    sudo python TMP102_read_temp.py
    Hint: Use SFTP or a USB flash drive to copy the script to a folder on the pi
  • Saving data to GoogleCreate a sheet
    1. If you don't have a Google account, create one.
    2. Create a new sheet and create a header in column A for date and column B for temp.
    3. Delete all the extra rows except for the header. The Pi will be appending new rows to the sheet, so you don't want the data to be far down the sheet.
    Example used in this demo: https://docs.google.com/spreadsheets/d/1DRIfCrX7HUyIeMmd2c0A6k1pNcZ7zQXFaNhOE0Rc3PM/edit?usp=sharing
    Set up authentication
    1. Obtain OAuth2 credentials by following the steps outlined by Google below. Follow steps 1-4, you only need the json file.
    2. Search the json file for "client_id". Save the text following "client_id", it will be used in the python script.
      "client_email": "284377770079-0o2pssk1b0qjjddi6rvag4h7i7rsl1on@developer.gserviceaccount.com",
      *note: the text in the file you download will be different
    3. Copy the json file to a folder on the Raspberry Pi.
    4. In the sheet created in the previous section, click File>Share. Select "Can Edit" and paste the "client-email" above into the email line.
    5. On the Pi, install the necessary software by typing the following on the command line:
      sudo apt-get install python-pip
      sudo pip install gspread oauth2client
      sudo apt-get install python-openssl
    Testing the connection
    1. Run the following script on the Pi from the same directory as the json file from the earlier step. The script uses the time from the Pi for the first column, so make sure the time is setup correctly using raspi-config.
      sudo python TMP102_google_sheet.py
    Automatically run
    1. You could modify the script to loop, or you could use a cron job so the I2C bus is released when not in use. Here is how to setup a cron job.
    2. Add how often you want the script to run using crontab:
    crontab -e
  • Add the following line to run every 10mn:
  • */10 * * * * cd /path/to/script && python /path/to/script >> /path/to/log 2>&1
    478 likes
    Statement: This post is only the personal view of the author and does not represent the opinions of ALLPCB.com.

    Burnell

    Leave A Message

    Follow

    I am a new beginner and looking for good info for study. The posted info is pretty good. I will share it with my friends.
    ThreadReply