Skip to content

Commit

Permalink
fix ghost channels when used for OUTPUT (#4)
Browse files Browse the repository at this point in the history
- fix ghost channels when using for OUTPUT
  - add disable/enable in setChannel()
- improve setChannel(), only write changed pins.
- update HC4067_performance.ino
- update readme.md
- update GitHub actions
- minor edits
  • Loading branch information
RobTillaart authored Apr 3, 2024
1 parent 0a2c5d8 commit e14e218
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 51 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# These are supported funding model platforms

github: RobTillaart
custom: "https://www.paypal.me/robtillaart"

3 changes: 2 additions & 1 deletion .github/workflows/arduino-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: arduino/arduino-lint-action@v1
with:
library-manager: update
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/arduino_test_runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ on: [push, pull_request]
jobs:
runTest:
runs-on: ubuntu-latest
timeout-minutes: 20

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
- run: |
# sudo sysctl vm.mmap_rnd_bits=28
gem install arduino_ci
arduino_ci.rb
5 changes: 3 additions & 2 deletions .github/workflows/jsoncheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ on:
jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: json-syntax-check
uses: limitusus/json-syntax-check@v1
uses: limitusus/json-syntax-check@v2
with:
pattern: "\\.json$"

11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.2.0] - 2024-04-03
- fix ghost channels when using for OUTPUT
- add disable/enable in setChannel()
- improve setChannel(), only write changed pins.
- update HC4067_performance.ino
- update readme.md
- update GitHub actions
- minor edits

----

## [0.1.2] - 2023-11-04
- update readme.md

Expand Down
18 changes: 13 additions & 5 deletions HC4067.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// FILE: HC4067.h
// AUTHOR: Rob Tillaart
// DATE: 2023-01-25
// VERSION: 0.1.2
// VERSION: 0.2.0
// PURPOSE: Arduino library for CD74HC4067 1 x 16 channel multiplexer and compatibles.
// URL: https://github.com/RobTillaart/HC4067



#include "Arduino.h"

#define HC4067_LIB_VERSION (F("0.1.2"))
#define HC4067_LIB_VERSION (F("0.2.0"))


class HC4067
Expand Down Expand Up @@ -42,16 +42,24 @@ class HC4067

void setChannel(uint8_t channel)
{
if ((channel & 0x0F) != _channel)
uint8_t _new = channel & 0x0F;
if (_new != _channel)
{
_channel = channel & 0x0F;
uint8_t _changed = _new ^ _channel;
uint8_t mask = 0x08;
uint8_t i = 3;
disable(); // prevent ghost channels.
while (mask)
{
digitalWrite(_pins[i--], (mask & _channel));
// only write changed pins. // AVR only?
if (mask & _changed)
{
digitalWrite(_pins[i--], (mask & _new));
}
mask >>= 1;
}
enable();
_channel = _new;
}
}

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023-2023 Rob Tillaart
Copyright (c) 2023-2024 Rob Tillaart

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
70 changes: 43 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ multiplexer / demultiplexer and compatible devices.
The HC4067 allows e.g one analog port read up to 16 different analog channels,
or one digital port to read the state of 16 buttons.

It is also possible to use the HC4067 to select an OUTPUT channel.
The signal pin can be connected to VCC (5V) or an IO pin set to OUTPUT.
Only the selected channel can show the HIGH level of the IO pin if set to HIGH.
Not selected pins will all be set to LOW.

The channel selection is done with four select lines **S0..S3**
The channel selection is done with four select lines **S0..S3**.

The device can be enabled/disabled by the enable line **E**
The device can be enabled/disabled by the enable line **E**.


#### Compatibles
Expand All @@ -40,14 +44,16 @@ Not tested, considered compatible.
- https://github.com/RobTillaart/HC4052 (2x4 mux)
- https://github.com/RobTillaart/HC4053 (3x2 mux)
- https://github.com/RobTillaart/HC4067 (1x16 mux)
- https://github.com/RobTillaart/MAX14661 (2x16 mux, I2C)
- https://tronixstuff.com/2013/08/05/part-review-74hc4067-16-channel-analog-multiplexerdemultiplexer/


## Hardware connection

Typical connection is to connect the four **select pins** to four IO Pins of your board.
Typical connection is to connect the four **select pins** to four IO pins of your board.

The optional **enablePin E** must be connected to GND if not used.
This way the device is continuous enabled.
The optional **enablePin E** must be connected to **GND** if not used.
This way the device will be continuous enabled.

Example multiplexing analog in.

Expand Down Expand Up @@ -76,6 +82,25 @@ Example multiplexing analog in.
```


#### Less Select lines

Note: the library does not meant for this mode, although it should work.
The GND-ed pins should be set to 255 (not tested).

It is possible to use less IO pins to connect to the S0..S3.
The ones not connected to an IO pin must be connected to GND (preferred).

| S0 | S1 | S2 | S3 | pins | notes |
|:-----:|:-----:|:-----:|:-----:|:------:|:-------:|
| IO | IO | IO | IO | 0-15 | default usage
| IO | IO | IO | GND | 0-7 |
| IO | IO | GND | GND | 0-3 |
| IO | GND | GND | GND | 0-1 |

Of course it is possible to set a Select pin to VCC instead of GND.
This will result in another subset of the Y pins to select from.


## Interface

```cpp
Expand All @@ -89,50 +114,41 @@ Set the 4 select pins and optional the enable pin.
If the enablePin == 255 it is considered not used.
- **void setChannel(uint8_t channel)** set the current channel.
Valid values 0..15, this value is not checked, only the lower 4 bits will be used.
- **uint8_t getChannel()** get current channel 0..15.
- **uint8_t getChannel()** returns the current channel 0..15.
The selected channel is also returned when the multiplexer is disabled.


#### Enable

These functions work only if enablePin is set in the constructor.

- **void enable()** idem.
- **void disable()** idem.
- **bool isEnabled()** idem.
Also returns true if enablePin is not set.
- **void enable()** enables the HC4067 to multiplex.
- **void disable()** disables the HC4067, no channel is selected.
- **bool isEnabled()** returns the current status of the HC4067.
Also returns true if the enablePin is not set in the constructor.


## Future

#### Must

- elaborate documentation
- links etc.

- documentation

#### Should

- optimizations
- performance setChannel
- investigate how to use with only 3 lines or 2 lines.
- set s3 / s2 to LOW always or so

- test performance **setChannel()** on ESP32 / fast board.
- should optimized version be AVR only?

#### Could

- next() and prev() as channel selector.
- internal channel variable needed.
- move code to .cpp file
- investigate
- can it be used as 16 channel OUTPUT (yes but)
- is it buffered?

- what to do with range borders? ==> stop?

#### Won't (unless requested)

- optimizations
- only do digitalWrite when changed? gain is minimal.
- now takes 24 micros on UNO if set.
- check channel in setChannel() ?
- return true if in range, false otherwise.
- move code to .cpp file


## Support
Expand Down
3 changes: 2 additions & 1 deletion examples/HC4067_16_buttons/HC4067_16_buttons.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

#include "HC4067.h"

HC4067 mp(4, 5, 6, 7);
HC4067 mp(4, 5, 6, 7); // no enable pin defined connect to GND.

const int inputPin = 10;
uint16_t values;


void setup()
{
Serial.begin(115200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@

// 4..7 channel select pins
// 8 = enable pin
HC4067 mp(4, 5, 6, 7, 8);
HC4067 mp(4, 5, 6, 7, 8);


uint32_t lastTime = 0;


void setup()
{
Serial.begin(115200);
Expand Down
6 changes: 4 additions & 2 deletions examples/HC4067_demo/HC4067_demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "HC4067.h"

HC4067 mp(4, 5, 6, 7);
HC4067 mp(4, 5, 6, 7); // no enable pin defined connect to GND.


void setup()
Expand All @@ -26,9 +26,11 @@ void loop()
for (uint8_t channel = 0; channel < 16; channel++)
{
mp.setChannel(channel);
Serial.println(analogRead(A0));
Serial.print(analogRead(A0));
Serial.print("\t");
delay(100);
}
Serial.println();
}


Expand Down
24 changes: 17 additions & 7 deletions examples/HC4067_performance/HC4067_performance.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,42 @@

#include "HC4067.h"

HC4067 mp(4, 5, 6, 7);
HC4067 mp(4, 5, 6, 7, 8);

uint32_t start, stop;


void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("HC4067 LIBRARY VERSION: ");
Serial.println(HC4067_LIB_VERSION);
Serial.println();
delay(100);

delay(1000);
start = micros();
mp.setChannel(10);
stop = micros();

for (uint8_t ch = 0; ch < 16; ch ++)
{
start = micros();
mp.setChannel(ch);
stop = micros();
Serial.print("Channel ");
Serial.print(ch);
Serial.print(": \t");
Serial.println(stop - start);
delay(100);
}

Serial.print("SetChannel: \t");
Serial.println(stop - start);
mp.setChannel(10);
delay(100);

start = micros();
mp.setChannel(10);
stop = micros();

Serial.print("SetChannel: \t");
Serial.print("\nSetChannel: \t");
Serial.println(stop - start);
delay(100);

Expand Down
22 changes: 22 additions & 0 deletions examples/HC4067_performance/output_0.2.0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

HC4067_performance.ino
HC4067 LIBRARY VERSION: 0.2.0

Channel 0: 24
Channel 1: 20
Channel 2: 28
Channel 3: 20
Channel 4: 32
Channel 5: 20
Channel 6: 24
Channel 7: 24
Channel 8: 36
Channel 9: 20
Channel 10: 24
Channel 11: 20
Channel 12: 32
Channel 13: 24
Channel 14: 24
Channel 15: 20

SetChannel: 8
2 changes: 1 addition & 1 deletion examples/HC4067_test/HC4067_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "HC4067.h"

HC4067 mp(4, 5, 6, 7, 8); // explicitly set enable pin
HC4067 mp(4, 5, 6, 7, 8); // explicitly set enable pin (8)


void setup()
Expand Down
Loading

0 comments on commit e14e218

Please sign in to comment.