2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
[![Arduino CI](https://github.com/RobTillaart/PCF8591/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
2021-12-23 08:26:13 -05:00
|
|
|
[![Arduino-lint](https://github.com/RobTillaart/PCF8591/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/PCF8591/actions/workflows/arduino-lint.yml)
|
|
|
|
[![JSON check](https://github.com/RobTillaart/PCF8591/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/PCF8591/actions/workflows/jsoncheck.yml)
|
2021-01-29 06:31:58 -05:00
|
|
|
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/PCF8591/blob/master/LICENSE)
|
|
|
|
[![GitHub release](https://img.shields.io/github/release/RobTillaart/PCF8591.svg?maxAge=3600)](https://github.com/RobTillaart/PCF8591/releases)
|
|
|
|
|
|
|
|
|
|
|
|
# PCF8591
|
|
|
|
|
2021-12-23 08:26:13 -05:00
|
|
|
Arduino Library for PCF8591 I2C 4 channel 8 bit ADC + 1 channel 8 bit DAC.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
## Description
|
|
|
|
|
2022-10-01 07:17:26 -04:00
|
|
|
**warning** during tests I could overclock the PCF8591 chip up to 650 KHz.
|
|
|
|
However it is only specified to run at 100 kHz.
|
|
|
|
After some time it was getting pretty hot and it broke down.
|
2021-01-29 06:31:58 -05:00
|
|
|
So overclocking is fun but not recommended.
|
|
|
|
|
|
|
|
PCF8591 has one 8 bit ADC on board for 4 channels. The ADC is 8 bit and quite fast.
|
2021-12-23 08:26:13 -05:00
|
|
|
At 100 KHz one gets \> 2000 reads per second for **analogRead()** and
|
2021-01-29 06:31:58 -05:00
|
|
|
\> 2000 writes per second for **analogWrite()**.
|
|
|
|
Note that most time is probably spend on I2C communication.
|
|
|
|
|
|
|
|
The auto-increment functionality is used in the **analogRead4()** function.
|
|
|
|
The library only supports it for the mode 0 (plain ADC, no differential).
|
|
|
|
The **lastRead()** function is needed to get access to the values.
|
|
|
|
First tests shows it is 2.6 x faster than 4 individual reads.
|
|
|
|
|
|
|
|
**analogRead4()** needs investigation in the future for the other modi.
|
|
|
|
|
|
|
|
|
|
|
|
## Interface
|
|
|
|
|
|
|
|
### Generic
|
|
|
|
|
2021-12-23 08:26:13 -05:00
|
|
|
- **PCF8591(const uint8_t address, TwoWire \*wire = &Wire)** constructor with I2C address,
|
|
|
|
default is 0x48, optional set the WireN I2C bus.
|
|
|
|
- **bool begin(uint8_t sda, uint8_t scl, uint8_t value = 0)** set wire pins for ESP series.
|
2021-01-29 06:31:58 -05:00
|
|
|
Also set initial value for the DAC. Returns **true** if successful.
|
2021-12-23 08:26:13 -05:00
|
|
|
- **bool begin(uint8_t value = 0)** Set initial value for the DAC. Returns **true** if successful.
|
|
|
|
- **bool isConnected()** test to see if chip can be reached.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
2022-11-21 15:33:51 -05:00
|
|
|
### ADC channels
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-10-01 07:17:26 -04:00
|
|
|
The PCF8591 has four 8 bit ADC channels. Values = 0..255.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-11-21 15:33:51 -05:00
|
|
|
- **void enableINCR()** used in analogRead4(). Could become private in the future.
|
2021-12-23 08:26:13 -05:00
|
|
|
- **void disableINCR()** idem.
|
|
|
|
- **bool isINCREnabled()** idem.
|
|
|
|
- **uint8_t analogRead(uint8_t channel, uint8_t mode = 0)** read one of the 4 analogue ports.
|
2021-01-29 06:31:58 -05:00
|
|
|
Default mode is single ports. For comparator modes test datasheet.
|
2021-12-23 08:26:13 -05:00
|
|
|
- **uint8_t analogRead4()** read all 4 channels in one call.
|
2021-01-29 06:31:58 -05:00
|
|
|
Uses **enableINCR()** to do that efficiently.
|
2021-12-23 08:26:13 -05:00
|
|
|
It is about 2.6 x faster than 4 individual **analogRead()**, although the latter
|
2021-01-29 06:31:58 -05:00
|
|
|
allows for optimized timing per channel.
|
|
|
|
Only 4x single ports mode supported for now, comparator modi needs investigation.
|
2022-11-21 15:33:51 -05:00
|
|
|
Returns **PCF8591_OK** or an error code.
|
2021-12-23 08:26:13 -05:00
|
|
|
- **uint8_t lastRead(uint8_t channel)** get last read value from cache.
|
2021-01-29 06:31:58 -05:00
|
|
|
This cache is filled both by **analogRead()** and **analogRead4()**. See example sketch.
|
|
|
|
|
|
|
|
|
2022-11-21 15:33:51 -05:00
|
|
|
### DAC channel
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-10-01 07:17:26 -04:00
|
|
|
The PCF8591 has one 8 bit DAC. output value 0..255 == 0..Vref Volts (datasheet).
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-12-23 08:26:13 -05:00
|
|
|
- **void enableDAC()** switch on the analogue output.
|
2022-11-21 15:33:51 -05:00
|
|
|
- **void disableDAC()** switch off the analogue output (high impedance). Sort of energy saving mode.
|
2021-12-23 08:26:13 -05:00
|
|
|
- **bool isDACEnabled()** check the modus operandi.
|
|
|
|
- **bool analogWrite(uint8_t value = 0)** writes a value 0..255 to the DAC. Check datasheet for voltage.
|
2022-10-01 07:17:26 -04:00
|
|
|
Note, this is a real voltage not a PWM signal like **analogWrite()** on an UNO.
|
2021-12-23 08:26:13 -05:00
|
|
|
- **uint8_t lastWrite()** get last written value from cache.
|
2022-11-21 15:33:51 -05:00
|
|
|
|
|
|
|
### Error codes
|
|
|
|
|
2021-12-23 08:26:13 -05:00
|
|
|
- **int lastError()** always check this value after a read / write to see if it was OK (== 0).
|
|
|
|
After the read the error value is reset to OK.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-11-21 15:33:51 -05:00
|
|
|
To elaborate
|
|
|
|
|
|
|
|
| error code | Value | Notes |
|
|
|
|
|:------------------------|:-------:|:--------|
|
|
|
|
| PCF8591_OK | 0x00 |
|
|
|
|
| PCF8591_PIN_ERROR | 0x81 |
|
|
|
|
| PCF8591_I2C_ERROR | 0x82 |
|
|
|
|
| PCF8591_MODE_ERROR | 0x83 |
|
|
|
|
| PCF8591_CHANNEL_ERROR | 0x84 |
|
|
|
|
| PCF8591_ADDRESS_ERROR | 0x85 |
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-12-23 08:26:13 -05:00
|
|
|
## Operations
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-12-23 08:26:13 -05:00
|
|
|
See examples.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
2021-12-23 08:26:13 -05:00
|
|
|
## Future
|
|
|
|
|
2022-11-21 15:33:51 -05:00
|
|
|
#### must
|
|
|
|
- improve documentation
|
|
|
|
|
|
|
|
#### should
|
2021-12-23 08:26:13 -05:00
|
|
|
- add / improve comparator modi support, datasheet (par.8.2 figure 4)
|
|
|
|
- int16_t readComparator10()
|
|
|
|
- int16_t readComparator30() - return type correct?
|
|
|
|
- int16_t readComparator31()
|
|
|
|
- int16_t readComparator32()
|
|
|
|
- set modi and read.
|
2022-11-21 15:33:51 -05:00
|
|
|
|
|
|
|
|
|
|
|
#### could
|
2021-12-23 08:26:13 -05:00
|
|
|
- **analogRead4()** needs investigation for the other modi.
|
|
|
|
- Does it work?
|
|
|
|
- Is it user understandable?
|
2022-11-21 15:33:51 -05:00
|
|
|
- good example
|
2021-01-29 06:31:58 -05:00
|
|
|
|