{"id":19072888,"url":"https://github.com/nickoala/sensor","last_synced_at":"2025-07-21T12:34:08.761Z","repository":{"id":57465548,"uuid":"42094818","full_name":"nickoala/sensor","owner":"nickoala","description":"Raspberry Pi Sensors","archived":false,"fork":false,"pushed_at":"2021-03-06T11:24:28.000Z","size":57,"stargazers_count":81,"open_issues_count":4,"forks_count":20,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-07-19T01:34:38.814Z","etag":null,"topics":["bmp180","ds18b20","htu21d","mcp3004","mcp3008","python","raspberry-pi","sensor","sht20"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nickoala.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-09-08T06:44:34.000Z","updated_at":"2025-06-20T14:38:04.000Z","dependencies_parsed_at":"2022-09-13T13:40:25.537Z","dependency_job_id":null,"html_url":"https://github.com/nickoala/sensor","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/nickoala/sensor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickoala%2Fsensor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickoala%2Fsensor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickoala%2Fsensor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickoala%2Fsensor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nickoala","download_url":"https://codeload.github.com/nickoala/sensor/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickoala%2Fsensor/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266302934,"owners_count":23908265,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-21T11:47:31.412Z","response_time":64,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bmp180","ds18b20","htu21d","mcp3004","mcp3008","python","raspberry-pi","sensor","sht20"],"created_at":"2024-11-09T01:44:30.253Z","updated_at":"2025-07-21T12:34:08.710Z","avatar_url":"https://github.com/nickoala.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Raspberry Pi Sensors\n\nThis is a **Python 3** package that enables **Raspberry Pi** to read various\nsensors.\n\nSupported devices include:\n- **DS18B20** temperature sensor\n- **BMP180** pressure and temperature sensor\n- **HTU21D** humidity and temperature sensor\n- **SHT20** humidity and temperature sensor\n- **MCP3004** A/D Converter (**MCP3008** also compatible)\n\nThe chief motivation for this package is educational. I am teaching a Raspberry\nPi course, and find it very troublesome for students having to download a\nseparate library every time they use another sensor. With this package, download\nonce and they are set (for my course, anyway). I hope you find it useful, too.\n\n## Installation\n\nIt is best to update Linux first.\n\n`sudo apt-get update`  \n`sudo apt-get dist-upgrade`\n\nInstall this package:\n\n`sudo pip3 install sensor`\n\nBut the `sensor` package would not work by itself. Communicating with sensors\noften requires some sort of serial protocol, such as **1-wire**, **I2C**, or\n**SPI**. You have to know which sensor speaks which, and set up Raspberry Pi to\ndo so.\n\n## Enable 1-Wire, I2C, or SPI\n\n`sudo raspi-config`, enter **Interfacing Options**, enable the protocols you\nneed.\n\n## Know your sensor's address\n\nUnlike many libraries out there, this library knows **no default bus number**\nand **no default device address**. I want learners to be explicitly aware of\nthose numbers, even if they are fixed.\n\nFor example:\n- **I2C** bus is numbered **1**\n- **SPI** bus is numbered **0**\n\nTo find out individual sensor's address:\n- For 1-wire sensors, go to `/sys/bus/w1/devices/`\n- For I2C sensors, use `i2cdetect -y 1`\n- For SPI sensors, you should know which CS pin you use\n\n## My sensors don't give simple numbers\n\nUnlike many libraries out there, this library does not return a simple Celcius\ndegree when reading temperatures, does not return a simple hPa value when\nreading pressure, does not return a simple RH% when reading humidity, etc.\nInstead, I return a **namedtuple** representing the quantity, which offers two\nbenefits:\n\n- No more conversion needed. Suppose you get a *Temperature* called `t`, you may\n  access the Celcius degree by `t.C` as easily as you do Fahrenheit by `t.F`.\n- Namedtuples may have methods. For example, a *Pressure* has a method called\n  `altitude()`, which tells you how high you are above mean sea level.\n\n## DS18B20\n\n- Temperature, 1-wire\n- To find out the sensor's address:\n\n    ```\n    $ cd /sys/bus/w1/devices/\n    $ ls\n    28-XXXXXXXXXXXX  w1_bus_master1\n    ```\n\nRead the sensor as follows:\n\n```python\nfrom sensor import DS18B20\n\nds = DS18B20('28-XXXXXXXXXXXX')\nt = ds.temperature()  # read temperature\n\nprint(t)    # this is a namedtuple\nprint(t.C)  # Celcius\nprint(t.F)  # Fahrenheit\nprint(t.K)  # Kelvin\n```\n\n## BMP180\n\n- Pressure + Temperature, I2C\n- Use `i2cdetect -y 1` to check address. It is probably `0x77`.\n\n```python\nfrom sensor import BMP180\n\n# I2C bus=1, Address=0x77\nbmp = BMP180(1, 0x77)\n\np = bmp.pressure()  # read pressure\nprint(p)            # namedtuple\nprint(p.hPa)        # hPa value\n\nt = bmp.temperature()  # read temperature\nprint(t)               # namedtuple\nprint(t.C)             # Celcius degree\n\np, t = bmp.all()  # read both at once\nprint(p)          # Pressure namedtuple\nprint(t)          # Temperature namedtuple\n\n# Look up mean sea level pressure from local observatory.\n# 1009.1 hPa is only for example.\na = p.altitude(msl=1009.1)\n\nprint(a)     # Altitude\nprint(a.m)   # in metre\nprint(a.ft)  # in feet\n```\n\n## HTU21D\n\n- Humidity + Temperature, I2C\n- Use `i2cdetect -y 1` to check address. It is probably `0x40`.\n\n```python\nfrom sensor import HTU21D\n\n# I2C bus=1, Address=0x40\nhtu = HTU21D(1, 0x40)\n\nh = htu.humidity()  # read humidity\nprint(h)            # namedtuple\nprint(h.RH)         # relative humidity\n\nt = htu.temperature()  # read temperature\nprint(t)               # namedtuple\nprint(t.F)             # Fahrenheit\n\nh, t = htu.all()  # read both at once\n```\n\n## SHT20\n\n- Humidity + Temperature, I2C\n- Use `i2cdetect -y 1` to check address. It is probably `0x40`.\n\n```python\nfrom sensor import SHT20\n\n# I2C bus=1, Address=0x40\nsht = SHT20(1, 0x40)\n\nh = sht.humidity()  # read humidity\nprint(h)            # namedtuple\nprint(h.RH)         # relative humidity\n\nt = sht.temperature()  # read temperature\nprint(t)               # namedtuple\nprint(t.C)             # Celsius\n\nh, t = sht.all()  # read both at once\n```\n\n## MCP3004\n\n- Analog sensors (e.g. photoresistor) cannot interface with Raspberry Pi\n  directly. They have to go through an A/D converter.\n\n```python\nfrom sensor import MCP3004\n\n# SPI bus=0, CS=0, V_ref=3.3V\nmcp = MCP3004(bus=0, addr=0, vref=3.3)\n\nmcp.voltage(0)  # read voltage on channel 0\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnickoala%2Fsensor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnickoala%2Fsensor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnickoala%2Fsensor/lists"}