# Adafruit 1-Wire GPIO Breakout - DS2413

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/015/559/medium800/microcontrollers_1551_LRG.jpg?1395509631)

Pins are precious in the microcontroller world. How many times have you needed just one more pin? Sure, you could step up to a Mega and get a whole bunch more, but what if you really just need one or two? The DS2413 breakout board is the perfect solution. Each DS2413 breakout has 2 open drain GPIO pins and a 1-Wire interface. Just one of these boards will give you 2 pins for the price of one. But you can keep expanding from there.  
  
You can put as many of these boards as you want on the the 1-wire bus and still control all of them with just one Arduino pin. Each chip has a 48-bit unique address, which means (_in theory_\*) you could have as many as 2 \* 2^48 pins controlled by just one Arduino pin! What could you control with 562 trillion pins?   
  
_\* In practice, you would run out of Arduino memory long before that. But it's fun to imagine!_

![](https://cdn-learn.adafruit.com/assets/assets/000/015/560/medium800/microcontrollers_2014_03_16_IMG_3168-1024.jpg?1395509659)

# Adafruit 1-Wire GPIO Breakout - DS2413

## Assembly & Wiring

The board comes pre-assembled and tested from our factory. We include optional headers for breadboard use. Or you can wire the board directly into your project.

# Headers
  
Installing the optional headers is a simple process and takes just a few minutes: ## Position the Header
Cut the header strip to length if necessary and place it in the breadboard (long pins down!). Position the breakout board over the header.![microcontrollers_2014_03_16_IMG_3158-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/015/561/medium640/microcontrollers_2014_03_16_IMG_3158-1024.jpg?1395510390)

![microcontrollers_2013_05_02_IMG_1741-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/015/562/medium640/microcontrollers_2013_05_02_IMG_1741-1024.jpg?1395510497)

## And Solder!
Solder each pin to assure a good electrical connection. If you are new to soldering, check out the [Adafruit Guide to Excellent Soldering.](http://learn.adafruit.com/adafruit-guide-excellent-soldering "Link: http://learn.adafruit.com/adafruit-guide-excellent-soldering")![microcontrollers_2014_03_16_IMG_3159-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/015/563/medium640/microcontrollers_2014_03_16_IMG_3159-1024.jpg?1395510615)

![microcontrollers_2014_03_16_IMG_3160-1024.jpg](https://cdn-learn.adafruit.com/assets/assets/000/015/564/medium640/microcontrollers_2014_03_16_IMG_3160-1024.jpg?1395510641)

# Wiring
Wiring to the Arduino is simple as well. Just power, ground and a signal wire:  
  

## Basic Wiring:

**- GND -\> Arduino GND  
- IO -\> Arduino GPIO pin (The example code uses pin 8)**  

Then connect a 4.7K ohm pullup resistor (included) from IO to 5v.![](https://cdn-learn.adafruit.com/assets/assets/000/015/566/medium800/microcontrollers_2014_03_16_IMG_3163-1024.jpg?1395511108)

## Wiring Multiple DS2413 Breakouts
To add more breakouts, just connect more to the same GND and IO pin. Additional pullup resistors are not required.![](https://cdn-learn.adafruit.com/assets/assets/000/015/567/medium800/microcontrollers_2014_03_16_IMG_3164-1024.jpg?1395511764)

# Adafruit 1-Wire GPIO Breakout - DS2413

## Use it!

Once connected to the onewire bus, the GPIO pins can be used for either input or output. The following pages will show you how to control these pins via the onwire library. We will also show you how to connect things to the open drain outputs of the GPIO pins.

![](https://cdn-learn.adafruit.com/assets/assets/000/015/586/medium800/microcontrollers_2014_03_16_IMG_3167-1024.jpg?1395579725)

# Adafruit 1-Wire GPIO Breakout - DS2413

## OneWire Library

![](https://cdn-learn.adafruit.com/assets/assets/000/084/039/medium800/microcontrollers_2014_03_16_IMG_3168-1024.jpg?1573525001)

# Download and Install

The DS2413 uses the Maxim/Dallas OneWire protocol. You can download the [OneWire library](http://www.pjrc.com/teensy/td_libs_OneWire.html) from the Arduino library manager.

Open up the Arduino library manager

![](https://cdn-learn.adafruit.com/assets/assets/000/084/038/medium800/microcontrollers_library_manager_menu.png?1573524862)

Search for the **OneWire** library and install it. You'll have to scroll down a bit to find the right one.

![](https://cdn-learn.adafruit.com/assets/assets/000/084/041/medium800/microcontrollers_onewire.png?1573525085)

# Library Documentation

Detailed documentation of the OneWire Library can be found at the PJRC site here:

[OneWire Library Documentation and Examples](http://www.pjrc.com/teensy/td_libs_OneWire.html)
# DS2413 Example Sketch

We have an example sketch that shows how to use the OneWire library to talk to the DS2413 GPIO pins here at GitHub: [https://github.com/adafruit/Adafruit\_Learning\_System\_Guides/tree/master/Adafruit\_DS2413](https://github.com/adafruit/Adafruit_Learning_System_Guides/tree/master/Adafruit_DS2413)

[DS2413 Example Sketch Download](https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/master/Adafruit_DS2413/DS2413/DS2413.ino)
https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Adafruit_DS2413/DS2413/DS2413.ino

# Adafruit 1-Wire GPIO Breakout - DS2413

## Open Drain GPIO

The DS2413 outputs are "Open Drain". What that means is that the output is the "Drain" of an N-channel FET:

![](https://cdn-learn.adafruit.com/assets/assets/000/015/590/medium800/microcontrollers_MOSFET.jpg?1395581357)

Internally, the "Source" of the FET is connected to Ground. But the Drain is left open. When switched on, the FET provides a path for current to flow from the output pin to ground.

![](https://cdn-learn.adafruit.com/assets/assets/000/015/589/medium800/microcontrollers_OpenDrain.jpg?1395580893)

Since there is no internal connection to VCC, the open drain output does not put out a voltage like an Arduino GPIO pin. You have to provide your own external pullup. While this may seem inconvenient, the open drain configuration allows for a lot of flexibility. Since the output voltage is not dependent on the operating voltage of the breakout, you can use a variety of power sources. The FETs in the DS2413 are capable of switching voltages up to 28v and up to 20mA.  
  
The diagram below from the [DS2413 data sheet](http://datasheets.maximintegrated.com/en/ds/DS2413.pdf "Link: http://datasheets.maximintegrated.com/en/ds/DS2413.pdf") shows how input and output connections should be made - with an external pullup to a local power source.

![](https://cdn-learn.adafruit.com/assets/assets/000/015/588/medium800/microcontrollers_4588.gif?1448048874)

For more information, refer to the DS2413 data sheet:

[DS2413 Data Sheet](http://datasheets.maximintegrated.com/en/ds/DS2413.pdf)
# Adafruit 1-Wire GPIO Breakout - DS2413

## Running the Example Code

# Download the example sketch from our [Github repository](https://github.com/adafruit/Adafruit_DS2413 "Link: https://github.com/adafruit/Adafruit\_DS2413"):
[Download Example Code](https://github.com/adafruit/Adafruit_Learning_System_Guides/tree/master/Adafruit_DS2413)
# Wiring:
Connect a LED to IOA and IOB as follows:  
  

- **Cathode** (short leg) of LED connected to the **GPIO pin**.  
- **Anode** (long leg) of LED connected to one end of a **resistor** \*.  
- Other end of the **resistor** connected to **5v**.  

  
_\* A 1K resistor is shown here as a 'safe' value. For maximum LED brightness, you can learn how to calculate the optimum resistance in this guide_: [http://learn.adafruit.com/all-about-leds](http://learn.adafruit.com/all-about-leds "Link: http://learn.adafruit.com/all-about-leds")  
![](https://cdn-learn.adafruit.com/assets/assets/000/015/593/medium800/microcontrollers_2014_03_16_IMG_3168-1024.jpg?1395586500)

Compile and upload the example sketch and you should see both LEDs blinking: 1 second on and 1 second off.

![](https://cdn-learn.adafruit.com/assets/assets/000/015/594/medium800/microcontrollers_2014_03_16_IMG_3167-1024.jpg?1395586514)

# Adafruit 1-Wire GPIO Breakout - DS2413

## Reading, Writing and Arithmetic

Reading and writing to the DS2413 is via the **read()** and **write()** functions of the Onewire library:

> ## myWire.read();
> Read a byte.  
> 
> ## myWire.write(num);
> Write a byte.

These functions read and write a byte at a time. So you need to use a little binary arithmetic to separate out the 2 bits corresponding to the 2 GPIO pins.  
  
In the example code, the 2 LEDs are flashed by alternately writing **0x0** and **0x3**.```
/* Write */
  bool ok = false;
  ok = write(0x3);
  if (!ok) Serial.println(F("Wire failed"));
  delay(1000);
  ok = write(0x0);
  if (!ok) Serial.println(F("Wire failed"));
  delay(1000);
```

## Binary and Hexadecimal
The **0x3** in the example is the hexadecimal equivalent to the binary number **B00000011**. In the DS2413, the low-order bit (the one to the far right) corresponds to IOA and the one next to it corresponds to IOB. Writing 0x3 writes a binary '1' to both pins and turns them both on.  
  
To turn on just IOA you could write **0x1** (B00000001). And to turn on just IOB, you could write **0x2** (B00000010).  
  
If you substitute the following code, the LEDs will flash alternately:```
/* Write */
  bool ok = false;
  ok = write(0x1);
  if (!ok) Serial.println(F("Wire failed"));
  delay(1000);
  ok = write(0x2);
  if (!ok) Serial.println(F("Wire failed"));
  delay(1000);
```

Or, if you prefer, you can use the binary representation instead:

```
/* Write */
  bool ok = false;
  ok = write(B00000001);
  if (!ok) Serial.println(F("Wire failed"));
  delay(1000);
  ok = write(B00000010);
  if (!ok) Serial.println(F("Wire failed"));
  delay(1000);
```

For more on integer constants in the different number bases, see the[Arduino Integer Constants](http://arduino.cc/en/Reference/IntegerConstants#.Uy7-NvldV8E)page.

## Reading GPIO Pins
Reading is a little trickier: You need to separate the individual pin values from the byte that is returned from the read(). A read can return one of 5 values:  
  

- **0x0 (B00000000) - Both pins LOW**  
- **0x1 (B00000001) - IOA = HIGH, IOB = LOW**  
- **0x2 (B00000010) - IOA = LOW, IOB = HIGH**  
- **0x3 (B00000011) - Both pins HIGH**  
- **0xFF (B11111111) - Read Failed!**

To extract the state of an individual pin, you will need to use a little [binary math](http://arduino.cc/en/Reference/BitwiseAnd#.Uy8DVfldV8E). In particular the " **bitwise AND**" operator: " **&**".  
  
If you AND the read value from the GPIO breakout with the bit pattern of the pin you are interested in, you will get a boolean " **TRUE**" if the pin is **HIGH** and a " **FALSE**" if the pin is **LOW**. The following code snippet prints "A" if IOA is HIGH and "B" if IOB is HIGH.```
uint8_t state = read();
  const int IOA = 0x1;
  const int IOB = 0x2;
  
  if (state == -1)
  {
    Serial.println(F("Failed reading the DS2413"));
  }
  else
  {
    if (state &amp; IOA)
    {
       Serial.println("A");
    }
    if (state &amp; IOB)
    {
       Serial.println("B");
    }
  }
```

# Adafruit 1-Wire GPIO Breakout - DS2413

## Downloads

# Files

- [Datasheet for the DS2413](http://www.adafruit.com/datasheets/DS2413.pdf "Link: http://www.adafruit.com/datasheets/DS2413.pdf")
- [Product page for the DS2413](http://www.maximintegrated.com/datasheet/index.mvp/id/4588 "Link: http://www.maximintegrated.com/datasheet/index.mvp/id/4588")
- [One-Wire overview](http://www.maximintegrated.com/app-notes/index.mvp/id/1796)
- [EagleCAD PCB files on GitHub](https://github.com/adafruit/Adafruit-DS2413-PCB)
- [Fritzing object in Adafruit Fritzing library](https://github.com/adafruit/Fritzing-Library)

# Schematic
![](https://cdn-learn.adafruit.com/assets/assets/000/015/639/medium800/microcontrollers_dsschem.png?1396022120)

# Fabrication Print
![](https://cdn-learn.adafruit.com/assets/assets/000/015/638/medium800/microcontrollers_dslayout.png?1396022090)


## Featured Products

### DS2413 1-Wire Two GPIO Controller Breakout

[DS2413 1-Wire Two GPIO Controller Breakout](https://www.adafruit.com/product/1551)
You're too cool for I2C, and SPI has so many wires, 8-bit parallel... how can that be fashionable!? You are a 1-Wire kinda gal, and you want more 1-Wire breakouts in your life to complement all of your great engineering. Well rock on, because here you go, it's a 1-Wire controller with...

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

## Related Guides

- [Adafruit Chainable DS18B20 Extender Breakout](https://learn.adafruit.com/adafruit-chainable-ds18b20-extender-breakout.md)
- [Installing TinyUF2 bootloader on RT10xx Microcontrollers](https://learn.adafruit.com/installing-the-uf2-bootloader-on-nxp-imx-rt10xx-microcontrollers.md)
- [Adafruit STEMMA Analog SPDT Switch](https://learn.adafruit.com/adafruit-stemma-analog-spdt-switch.md)
- [Adafruit Sparkle Motion Stick](https://learn.adafruit.com/adafruit-sparkle-motion-stick.md)
- [Adafruit LPS25 and LPS22 Barometric Pressure and Temperature Sensors](https://learn.adafruit.com/adafruit-lps25-pressure-sensor.md)
- [Qualia S3 Fireplace](https://learn.adafruit.com/qualia-s3-fireplace.md)
- [Adafruit VL53L1X Time of Flight Distance Sensor](https://learn.adafruit.com/adafruit-vl53l1x.md)
- [How to program a Zumo Robot with Simulink](https://learn.adafruit.com/zumo-robot-control-with-simulink.md)
- [Adafruit NeoKey BFF](https://learn.adafruit.com/adafruit-neokey-bff.md)
- [USB C Dock](https://learn.adafruit.com/usb-c-dock.md)
- [Adafruit RS232 Pal](https://learn.adafruit.com/adafruit-rs232-pal.md)
- [Ethernet for CircuitPython with Wiznet5K](https://learn.adafruit.com/ethernet-for-circuitpython.md)
- [Bluetooth Temperature & Humidity Sensor](https://learn.adafruit.com/bluetooth-temperature-and-humidity-sensor.md)
- [Adafruit Metro M7 1011 with AirLift](https://learn.adafruit.com/adafruit-metro-m7-with-airlift.md)
- [Animated NeoPixel Glow Fur Scarf](https://learn.adafruit.com/animated-neopixel-gemma-glow-fur-scarf.md)
