Communicating With One-Wire Devices

  • Onion Omega2 Documentation
  • Getting Started
    • First Time Setup
      • The Video
      • Unboxing and Getting the Hardware Ready
      • Using the Setup Wizard
      • The Setup Wizard Didn’t Work!
    • First Time Setup using the Command Line
      • Unboxing and Getting the Hardware Ready
      • Preparing to Connect
      • Connect to the Omega’s Command Line
      • Update the Omega’s Firmware
      • Installing the Console
      • This Didn’t Work!
    • Troubleshooting
      • My Omega won’t fit into the Dock!
      • My Omega won’t connect to my WiFi Network
      • My Omega still won’t connect to my WiFi Network and I’ve made sure my WiFi password is correct
      • My Omega doesn’t reboot after installing the update, it just turns off
    • Using the Console
    • Using the Command Line
    • Your Omega’s Name
    • Powering the Omega with No Dock
    • Fix Network IP Address Collisions
      • What is an IP Address Collision?
      • Fixing the Collision
  • Hardware Overview
    • Onion Omega2
      • The Omega2 at a Glance
      • Processor Datasheet
      • The Pins
      • The Operating system
      • The Omega LED
      • Reset GPIO
      • The WiFi Antenna
      • The U.FL Connector
      • Mechanical Drawing
    • Onion Omega2+
      • The Omega2 at a Glance
      • Processor Datasheet
      • The Pins
      • The Operating system
      • The Omega LED
      • Reset GPIO
      • MicroSD Card Slot
      • The WiFi Antenna
      • The U.FL Connector
      • Mechanical drawing
    • Onion Omega2S & Omega2S+
      • Variants
      • The Pins
      • Datasheet & Other Resources
      • Differences from Omega2
      • Development Kit
      • Ordering Information
      • Datasheet
    • Onion Omega2 Pro
      • Getting Started with the Omega2 Pro
      • Hardware Designs, Measurements, Images, and More
      • The Omega2 Pro at a Glance
      • The Expansion Header
      • LiPo Battery Support
      • Full-color Notification LED
      • Node-Red
      • The Operating system
      • The Amber Status LED
      • The Blue WiFi Status LED
      • The MicroUSB Port
      • Power Switch
      • Reset button
      • Omega USB Port
      • The WiFi Antenna
      • The U.FL Connector
      • Software
    • Onion Omega2 Dash
      • Getting Started with the Omega2 Dash
      • Hardware Designs, Measurements, Images, and More
      • The Omega2 Dash at a Glance
      • Features and Specifications
      • The Expansion Header
      • The Operating system
      • The Status LEDs
      • The MicroUSB Port
      • Power Switch
      • Reset button
      • Omega USB Port
      • The WiFi Antenna
      • The U.FL Connector
      • Software
    • Onion Omega2 LTE
      • Getting Started with the Omega2 LTE
      • Hardware Designs, Measurements, Images, and More
      • The Omega2 LTE at a Glance
      • Features & Specifications
      • LTE Modem & Variants
      • The Expansion Header
      • The Operating system
      • The Status LEDs
      • The USB-C Port
      • LiPo Battery Support
      • Power Switch
      • Reset button
      • The WiFi Antenna
      • The U.FL Connector
      • Software
    • Expansion Dock
      • The Hardware
      • Connecting an Omega
      • The Expansion Header
      • The MicroUSB Port
      • Power Switch
      • Reset button
      • Omega USB Port
      • RGB LED
      • Mechanical Drawings
      • Using the Dock
    • Power Dock
      • The Hardware
      • Connecting an Omega
      • The Expansion Header
      • Micro-USB Port
      • The Power Switch
      • Battery Level Indicator LEDs
      • The Battery Connector
      • Reset button
      • Omega USB Port
      • Differences from the Expansion Dock
      • Using the Power Dock
      • Checking the Battery Level
    • Power Dock 2
      • The Hardware
      • Connecting an Omega
      • The Expansion Header
      • Micro-USB Port
      • The Power Switch
      • Battery Level Indicator LEDs
      • Battery Level ADC
      • The Battery Connector
      • Reset button
      • Omega USB Port
      • Differences from the Expansion Dock
      • Differences from the original Power Dock
      • Using the Power Dock
      • Checking the Battery Level
    • Mini Dock
      • The Hardware
      • Connecting an Omega
      • The MicroUSB Port
      • Power Switch
      • Reset button
      • Omega USB Port
      • Mechanical Drawings
    • Arduino Dock 2
      • The Hardware
      • Connecting an Omega
      • The Expansion Header
      • The ATmega Headers
      • The MicroUSB Port
      • DC Barrel Jack
      • Reset button
      • Micro-Controller Reset Button
      • Omega USB Port
      • Omega to ATmega MCU Connections
      • Mechanical Drawings
      • Using the Dock
      • Programming the Arduino Micro-Controller
      • Communicating over UART
      • Communicating with I2C
    • Breadboard Dock
      • The Hardware
      • Connecting an Omega
      • The Breadboard Header
      • The MicroUSB Port
      • Reset button
    • Relay Expansion
      • The Hardware
      • Connecting to a Dock
      • The Relays
      • The Screw Terminals
      • The Address Switch
      • Mechanical Drawings
      • Using the Relay Expansion
    • PWM Expansion
      • The Hardware
      • Connecting to a Dock
      • The PWM Channels
      • Connecting Servos
      • The DC Barrel Jack Adapter
      • The Oscillator
      • Mechanical Drawings
      • Using the PWM Expansion
    • OLED Expansion
      • The Hardware
      • Connecting to a Dock
      • The Screen
      • Understanding the Display
      • Mechanical Drawings
      • Using the OLED Expansion
    • Ethernet Expansion
      • The Hardware
      • Connecting to a Dock
      • Connecting an Ethernet Cable
      • Mechanical Drawings
      • Using the Ethernet Expansion
    • NFC & RFID Expansion
      • The Hardware
      • Connecting to a Dock
      • The Antenna
      • Using the RFID & NFC Expansion
    • ADC Expansion
      • The Hardware
      • Connecting to a Dock
      • Analog Input Headers
      • Grove Connectors
      • The Address Switch
      • Using the ADC Expansion
    • Proto Expansion
      • The Hardware
      • Connecting to a Dock
      • Solder-Friendly Header
      • Using the Expansion
    • GPS Expansion
      • The Hardware
      • Connecting to a Dock
      • The ublox Chip and Antenna
      • USB Connector
      • Using the GPS Expansion
    • Dock Compatibility
  • Using the Omega
    • Using the Omega’s GPIOs
      • GPIO Electrical Ratings
      • Multiplexed Pins
      • Important & Special Pins
      • Pins Important for Booting the Omega
      • SPI Pins & Onboard Flash Storage
      • LED & Reset GPIOs
      • Pin Behaviour during Boot
      • From the Command Line
      • Fast-GPIO
    • The Omega’s LED
      • The LED’s Name
      • LED Trigger Modes
    • Using USB Storage
      • USB Storage and Linux
      • Using USB storage
      • Unmounting
      • Changing Mounting Options
      • Changing the default mount point
    • Using a MicroSD Card
      • Which SD Cards are Supported
      • Omega2S SD Pins
      • Inserting a MicroSD Card into the Omega2+
      • Storage Devices and Linux
      • Accessing the MicroSD Card
      • Safely Removing a MicroSD Card
      • Removing a MicroSD Card on the Omega2+
    • Installing the Console
    • Transferring Files to the Omega
      • Using the Editor App in the Onion Console
      • Using Windows
      • Using Linux or Mac OS X
    • Connecting to Cloud Platforms
      • AWS IoT
      • Losant
      • Ubidots
      • IBM Watson IoT
      • Google Cloud IoT Core
      • Blynk
    • Rebooting your Omega
      • Using a Command
      • With the Reset button
    • Updating the Omega
      • How do I know if I need to update my Omega?
      • Performing the Upgrade
      • Getting your firmware version
      • Checking for New Firmware Versions
      • Upgrading to the Latest Version
      • Force Update/Reflash
      • Help with oupgrade
    • Factory Reset
      • What does a Factory Reset Actually Do?
      • Using a Command
      • With the Reset button
      • A note about Factory Resets and RSA Key Warnings
    • Playing Audio with USB Devices
      • Install the Required Packages
      • Adjusting Volume
      • Playing Audio
      • Recording Audio
    • Flashing the Arduino Dock
      • Prerequisites
      • Doing the Actual Flashing
      • Wireless Flashing with the Arduino IDE
      • Manually Flashing on the Command line
    • Pin Multiplexing
    • Generating PWM signals
      • Hardware Generated PWM Signals
      • Software Generated PWM Signals
      • Expansion / external IC generated
      • Programmatic Hardware PWM
    • Communicating with I2C Devices
      • The Omega & I2C
      • Controlling I2C Devices from the Command line
      • More Resources
      • Moving Beyond the Command line
    • Communicating with SPI Devices
      • What is SPI?
      • The Omega & SPI
      • Using Python
      • Using C Programs
    • Communicating with Serial Devices
      • What is a UART?
      • The Omega & UART
      • Using the Command Line
      • Using the screen Command
      • Using Python
    • Communicating with One-Wire Devices
      • The One-Wire Protocol
      • The Omega & One-Wire
      • Connecting the Hardware
      • Registering the One-Wire Master
      • Finding One-Wire Slave Devices
      • Reading from a One-Wire Device
  • Software
    • What is OPKG?
      • How to use the Package Manager
      • The Onion Package Repo
      • You don’t Have a Package I Want/Need!
      • Enable the Official OpenWRT Package Repos
    • Installing and Using Git
      • Installing Git
      • Using Git
    • Installing and Using Python
      • Installing Python
      • Getting More Python Modules
      • Using opkg to Install Python Modules
      • Using pip to Install Python Modules
      • Using Python
      • Onion Python Modules
      • Going Further
    • Installing and Using NodeJS
      • Installing NodeJS
      • Using NodeJS
      • npm
      • Using npm
      • Going further
    • Installing and Using PHP
      • Installing PHP
      • Using PHP
      • Going Further
    • Node-Red on the Omega2 Pro
      • Installing Node-RED
      • Example Flows
      • Installing Additional Node Packages
      • Installing Node Packages that require Compilation
  • Automation
    • Running a Command on Boot
      • Implementation
      • Troubleshooting
    • Running a Command on a Schedule
      • What is cron?
      • Using cron
      • Troubleshooting
  • The Omega and Connectivity
    • The Omega & Wireless Connectivity
      • Hosting a WiFi Access point
      • Connected to a WiFi network
      • Connected to a WiFi network AND hosting a WiFi Access Point
      • Sharing Internet Access with Your Omega’s Access Point
    • The Omega’s IP Address
      • Finding the IP Address when Connected to a WiFi network
      • Finding the IP Address of the Omega on it’s own AP
      • Changing the Omega’s IP Address on the AP network
    • The Ethernet Port
      • Ethernet Client: Connecting to a Network through Ethernet
      • Ethernet Host: Acting as the Host on a Wired Network
      • Ethernet Activity LED
    • Using the Ethernet Expansion
      • Learn More about the Omega’s Ethernet Port
      • Going Further with the Ethernet Expansion
  • Networking with the Omega
    • The Omega as a Router
      • Overview
      • Step 1: Setup the Hardware
      • Step 2: Setup the Omega
      • Step 3: Changing the Settings of the Omega’s WiFi Access Point
      • Step 4: Enabling Packet Routing
      • Step 5: Using the Omega Router
    • WiFi Range Extender
      • Overview
      • Step 1: Connect the Omega to the router
      • Step 2: Firewall Settings
      • Step 3: Use Your Omega WiFi Range Extender
    • Ethernet Bridge
      • Overview
      • Step 1: Connect Omega WiFi to the Internet
      • Step 2: Configure the Omega’s Ethernet Port
      • Step 3: Configure your Device to use Ethernet
  • Using the Omega Expansions
    • Using the Relay Expansion
      • Example Circuit
      • Using the Command Line
      • Using Multiple Relay Expansions by Changing the Dip-Switch Settings
      • Using the Libraries
    • Using the PWM Expansion
      • Pulse Width Modulation
      • Using the Command Line
      • Using the Libraries
    • Using the OLED Expansion
      • Command Usage
      • Command and Option Table
      • Options
      • Commands
      • Drawing Images on the Display
      • Supported Characters
      • Using the Libraries
    • Using the Ethernet Expansion
    • Using the GPS Expansion
      • Reading the Raw Output of the GPS Expansion
      • Reading NMEA Data Using ogps
      • Usage
    • Using the RFID & NFC Expansion
      • Supported Card Types
      • Installation
      • Available Commands
      • Scanning RFID/NFC Tags
      • Using Mifare Ultralight Cards
      • Using Mifare Classic Cards
      • RFID/NFC Expansion Availability
    • Using the ADC Expansion
      • The Channels
      • Digital Grove Connector
      • The Address Switch
      • Using the Command Line
  • Advanced
    • Extending the Omega’s Available Memory
      • What is a Swap File?
      • The Setup Procedure
      • Going Further
      • Summary
    • Manual Firmware Installation
      • Step 1: Downloading the Firmware Image
      • Step 2: Upgrading your Omega to the Downloaded Firmware
      • Additional sysupgrade Options
    • Device MAC Addresses
      • MAC Address Allocation
      • Reading the MAC Address
      • Programmatically Generate a Unique ID based on the Device’s MAC Address
    • Booting from External Storage
      • Warnings
      • The pivot-overlay Procedure
      • Requirements
      • Format Your Storage Device to ext4
      • Mounting the External Storage Device
      • Duplicating the /overlay Directory
      • Automatically Mount /overlay on Startup
      • Verify The New Filesystem
    • Compiling C on the Omega
      • Limitations
      • Installing the Compiler
      • Compiling a C Program
      • Compiling a C++ Program
      • Going Further
    • Cross-Compiling for the Omega
      • Setting Up the Build System
      • Cross Compiling
  • The Bootloader
    • Firmware Flashing With Web Recovery Mode
      • Step 1: Ingredients
      • Step 2: Downloading Firmware
      • Step 3: Configuring computer’s ethernet network
      • Step 4: Activating Web Recovery Mode
      • Step 5: Actual Flashing
      • Going Further
    • Firmware Flashing from USB Storage
      • Step 1: Ingredients
      • Step 2: Downloading Firmware
      • Step 3: Activating Recovery mode from USB storage
      • Step 4: Actual Flashing
      • Going Further
    • Updating the Bootloader
      • Step 1: Get Everything Together
      • Step 2: Downloading Bootloader Binary
      • Step 3: Configuring computer’s ethernet network
      • Step 4: Activating Web Recovery Mode
      • Step 5: Flash the New Bootloader
      • Checking your Bootloader Version
  • The Command Line
    • Connecting to the Omega’s Command Line
      • Connecting with SSH
      • SSH on a Mac device
      • SSH on a Linux device
      • SSH on a Windows Device
      • Using SSH Key Pairs
      • Connecting via Serial
      • Serial on a Mac Device
      • Serial on a Windows Device
      • Serial on a Linux Device
    • Connecting To WiFi Networks
      • Using wifisetup
      • Entering Network Info Manually
      • Using the wifisetup Command-Line Arguments
      • Disabling the WiFi Client Interface
      • Disabling the WiFi Radio Completely
    • Adjusting the AP Network
      • Alternative Method 1: Editing the Configuration File to Adjust the AP Network
      • Alternative Method 2: Using wifisetup
    • Updating the Omega
      • How do I know if I need to update my Omega?
      • Performing the Upgrade
      • Getting your firmware version
      • Checking for New Firmware Versions
      • Upgrading to the Latest Version
      • Force Update/Reflash
      • Help with oupgrade
    • Developing using the Command Line
      • Overview
      • Prerequisites
      • Controlling the LED from the Terminal
      • Writing a Shell Script in the Terminal using vi
      • Running your Script in the Terminal
  • The Console
    • Installing the Console
      • Installing Using the Setup Wizard
      • Installing Using the Command Line
    • Accessing the Console
    • Connecting to WiFi
      • Managing your WiFi Networks
    • Adjusting the Omega’s WiFi AP Network
    • Updating the Omega using the Console
      • The Omega Firmware
    • Installing Additional Apps
    • Developing using the Console
      • Overview
      • Prerequisites
      • Controlling the LED from the Terminal App
      • Writing a Shell Script in the Editor App
      • Running your Script in the Terminal App
  • Linux Introduction
    • Linux for Omega Beginners
      • What Is Linux?
      • What’s LEDE?
    • The Command Line Interface
    • Exploring The File System
      • How To Navigate In Linux
      • How To Create/Delete Files And Directories
    • Redirection
      • Input/Output Redirection
      • Piping
    • Shell Scripting
    • Ownership and Permissions
    • Intro to UCI
      • General Usage
      • show
      • add
      • set
      • get
      • Example - Changing the Omega’s LED Pattern
  • Firmware Reference
    • Known Firmware Issues
      • Issue Listing
      • Console Issue Listing
    • Omega Firmware Changelog
  • Software Reference
    • C Programming Language
    • Python Programming Language
    • I2C C Library
      • Linux and I2C
      • The C Library
      • Functions
      • Read a Single Byte - int i2c_readByte (int, int, int, int)
      • Read Multiple Bytes - int i2c_read(int, int, int, uint8_t*, int)
      • Write Buffer to Address - int i2c_writeBuffer (int, int, int, uint8_t*, int)
      • Directly Write to Address - int i2c_writeBytes (int, int, int, int, int)
    • PWM Expansion C Library
      • Programming Flow
      • PWM Signal Refresher
      • The C Library
      • Functions
      • Initialization Function - int pwmDriverInit ()
      • Check for Initialization - int pwmCheckInit (int*)
      • Generate a PWM Signal - int pwmSetupDriver (int, float, float)
      • Set PWM Signal Frequency - int pwmSetFrequency (float)
      • Disabling the Oscillator - int pwmDisableChip ()
    • Relay Expansion C Library
      • Programming Flow
      • I2C Device Address
      • The C Library
      • Functions
      • Initialization Function - int relayDriverInit (int)
      • Check for Initialization - int relayCheckInit (int, int*)
      • Set Relay State - int relaySetChannel (int, int, int)
      • Set State for both Relays - int relaySetAllChannels (int, int)
      • Read Relay State - int relayReadChannel (int, int, int*)
    • OLED Expansion C Library
      • Programming Flow
      • Understanding the Display
      • The C Library
      • Functions
      • Initialization Function - int oledDriverInit ()
      • Functions to Adjust Settings
      • Turn the Screen On and Off - int oledSetDisplayPower (int)
      • Invert Display Colours - int oledSetDisplayMode (int)
      • Set the Display Brightness - int oledSetBrightness (int)
      • Dim the Display - int oledSetDim (int)
      • Set Memory Mode - int oledSetMemoryMode (int)
      • Set Column Addressing - int oledSetColumnAddressing (int, int)
      • Set Columns for Text - int oledSetTextColumns ()
      • Set Columns for Images - int oledSetImageColumns ()
      • Set Cursor Position
      • Set Cursor Position by Character Column - int oledSetCursor (int, int)
      • Set Cursor Position by Pixel - int oledSetCursorByPixel (int, int)
      • Clear the Screen - int oledClear ()
      • Writing Text to the Display
      • Write a Single Byte - int oledWriteByte (int)
      • Write a Single Character - int oledWriteChar (char)
      • Write a String - int oledWrite (char*)
      • Drawing Images on the Display
      • Read Image Data - int oledReadLcdFile (char*, uint8_t*)
      • Write Image Data to the Display - int oledDraw (uint8_t *buffer, int bytes)
      • Scrolling the Display Contents
      • Horizontal Scrolling - int oledScroll (int, int, int, int)
      • Diagonal Scrolling - int oledScrollDiagonal (int, int, int, int, int, int, int)
      • Stop Scrolling - int oledScrollStop ()
    • I2C Python Module
      • Linux and I2C
      • The Python Module
      • Programming Flow
      • Functions
      • Initialization - onionI2C.OnionI2C()
      • Reading Bytes - readBytes()
      • Write a Single Byte - writeByte()
      • Write a List of Bytes - writeBytes()
      • Write a List of Bytes without Specifying an Address - write()
    • SPI Python Module
    • Onion GPIO Python Module
      • Programming Flow
      • Functions
      • Constructor - onionGpio.OnionGpio()
      • GPIO Direction
      • Reading the Current Direction - getDirection()
      • Setting the Direction
      • Set Pin to Input- setInputDirection()
      • Set Pin to Output - setOutputDirection()
      • GPIO Value
      • Reading the Value - getValue()
      • Setting the Value - setValue()
    • PWM Expansion Python Module
      • Programming Flow
      • PWM Signal Refresher
      • The Python Module
      • Functions
      • Initialization Function - driverInit()
      • Check for Initialization - checkInit()
      • Generate a PWM Signal - setupDriver()
      • Set PWM Signal Frequency - setFrequency()
      • Disabling the Oscillator - disableChip()
    • Relay Expansion Python Module
      • Programming Flow
      • I2C Device Address
      • The Python Module
      • Functions
      • Initialization Function - driverInit()
      • Check for Initialization - checkInit()
      • Set Relay State - setChannel()
      • Set State for both Relays - setAllChannels()
      • Read Relay State - readChannel()
    • OLED Expansion Python Module
      • Programming Flow
      • Understanding the Display
      • The Python Module
      • Functions
      • Initialization Function - driverInit()
      • Functions to Adjust Settings
      • Turn the Screen On and Off - setDisplayPower()
      • Invert Display Colours - setDisplayMode()
      • Set the Display Brightness - setBrightness()
      • Dim the Display - setDim()
      • Set Memory Mode - setMemoryMode()
      • Set Column Addressing - setColumnAddressing()
      • Set Columns for Text - setTextColumns()
      • Set Columns for Images - setImageColumns()
      • Set Cursor Position
      • Set Cursor Position by Character Column - setCursor()
      • Set Cursor Position by Pixel - setCursorByPixel()
      • Clear the Screen - clear()
      • Writing Text to the Display
      • Write a Single Byte - writeByte()
      • Write a Single Character - writeChar()
      • Write a String - write()
      • Drawing Images on the Display
      • Displaying Images from a File - drawFromFile()
      • Scrolling the Display Contents
      • Horizontal Scrolling - scroll()
      • Diagonal Scrolling - scrollDiagonal()
      • Stop Scrolling - scrollStop()
  • Published with bookdown
Onion Omega2 Documentation This documentation is for Onion firmware v0.3.4 and earlier.For the latest firmware based on OpenWRT 23.05, visit our new documentation site. ×

Communicating with One-Wire Devices

The One-Wire protocol is a bus-based protocol that, as the name implies, uses jsut one data wire to transmit data between devices. It allows controllers and processors like the Omega2 to easily communicate with peripheral devices like:

  • Sensors, such as temperature, humidity
  • Programmable Input/Output chips
  • Small relays

The One-Wire Protocol

One Wire is similar to the I2C protocol (which is coincidentally sometimes called TWI - Two Wire Interface). One Wire has a lower data transmission rate than I2C but it makes up for it with a longer range.

It follows a master-slave architecture with each bus allowing for one master, in this case the Omega, and many slave devices. Every device type has its own unique single-byte (8 bit) identifier, eg. 0x8f. Each device in turn has its own unique 8-byte (64-bit) serial number that includes a byte to describe the device type, known as the family code, as the Least Significant Byte (LSB).

An example of a One-Wire device serial number is shown below:

one-wire-serial-number

One Wire is also referred to as 1W, 1-Wire, W1 etc.

The Omega & One-Wire

Interacting with One-Wire devices with the Omega is slightly different from I2C, SPI, and Serial devices, but you’ll see that it’s not a big deal. Since there is no dedicated hardware One-Wire controller on the Omega, your One-Wire device can be connected to any GPIO. We will then register a One-Wire master in Linux associated with the selected GPIO that will allow us to communicate with the One-Wire slave devices.

Note that you need to be on firmware b151 or higher!

Connecting the Hardware

One-Wire devices will have three available connectors:

  • Vcc (usually 3.3V)
  • GND
  • Data Line

Take a look at your specific sensor’s datasheet to identify the pins and determine the recommended voltage.

Make the following connections to your Omega:

Pin Omega Connection
Vcc 3.3V
GND GND
Data GPIO19

Note that making these connections is very easy if you have a Expansion, Power, or Arduino Dock since they all expose the Omega’s GPIOs.

Most GPIOs will work, but for now, let’s use GPIO19. Some One-Wire devices will require a pull-up resistor on the Data line. For example, the popular DS18B20 temperature sensor, requires a 4.7 kΩ pull-up resistor on the Data line to operate properly. Some One-Wire devices have built-in pull-up resistors or can require different resistance values, check the datasheet of your device to be sure!

A pull-up resistor is a connection between, in this case, the data line and the voltage line. When the Data line is inactive, the pull up resistor will “pull” the signal to a logical high. Then when the Data line goes active, it will override the pull-up. It essentially ensures the logical level is always valid.

Registering the One-Wire Master

We will need to let our Linux operating system know that we intend to act as a One-Wire Master on GPIO19. So let’s run the following command:

insmod w1-gpio-custom bus0=0,19,0

This command does the following:

  • Tells Linux to load the w1-gpio-custom kernel module that will allow the Omega to act as a One-Wire master
  • It defines that this will be bus0
  • The 0,19,0 means:
    • This is for bus number 0
    • Use GPIO19 as the data pin to communicate with the One-Wire devices
    • The final 0 indicates that we will not be setting the data pin to open drain mode

If this command is successful, the following folder will become available:

/sys/devices/w1_bus_master1

Take a look inside this directory, it will be our One-Wire command centre!

Removing a One-Wire Master

If you’re done using your One-Wire device and would like to have your GPIO back, you can disable the One-Wire Master by running the following command:

rmmod w1-gpio-custom

Finding One-Wire Slave Devices

Now let’s use the new /sys/devices/w1_bus_master1 directory to find our slave devices.

First let’s check to see if there are any slave devices at all:

cat /sys/devices/w1_bus_master1/w1_master_slave_count

The output will be a number that will tell us how many slave devices are connected:

  • If it is a 1, you already have your device plugged in and you’re good to go.
  • If you see a 0, go ahead and plug in your device.
    • The One-Wire bus master kernel module scans the data pin every 10 seconds for new devices so wait a little while and try again

Finding your Specific Device

If your check of the slave count file reads 1, your device has been detected. Run ls /sys/devices/w1_bus_master1 and you should see a directory that looks something like this: 28-000123456789. That’s the directory of your slave device and it is based on the slave’s unique serial number.

Note that each device will have a different serial number, so yours might look a little different. This makes it a little difficult to use One-Wire devices programmatically, but don’t worry there’s a solution!

Running:

cat /sys/devices/w1_bus_master1/w1_master_slaves

will print a (newline delimited) list of the serial numbers of all connected One-Wire slaves!

Reading from a One-Wire Device

Reading from an attached One-Wire device is very simple, just run the following:

cat /sys/devices/w1_bus_master1/<DeviceID>/w1_slave

where <DeviceID> is the serial number of your One-Wire device.

Using the DS18B20 temperature sensor from the section above the command would be:

cat /sys/devices/w1_bus_master1/28-000123456789/w1_slave

And it will print something like:

b1 01 4b 46 7f ff 0c 10 d8 : crc=d8 YES b1 01 4b 46 7f ff 0c 10 d8 t=27062

Where the final t=27062 indicates the temperature is 27.062 ˚C.

To trim and format the output so just the temperature is returned:

root@Omega-2970:/# awk -F= '/t=/ {printf "%.03f\n", $2/1000}' /sys/devices/w1_bus_master1/28-000123456789/w1_slave 27.062

Từ khóa » W1-gpio Documentation