Skip to content

Commit

Permalink
Merge pull request #2 from RobTillaart/develop
Browse files Browse the repository at this point in the history
- fix ghost channels when using for OUTPUT
  - add disable/enable in setChannel()
- update setChannel() performance.
- update readme.md
- update GitHub actions
- minor edits
  • Loading branch information
RobTillaart authored Apr 3, 2024
2 parents 438d180 + bbe7e9e commit ec2586b
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 25 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$"

10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ 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()
- update setChannel() performance.
- update readme.md
- update GitHub actions
- minor edits

----

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

Expand Down
28 changes: 20 additions & 8 deletions HC4051.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// FILE: HC4051.h
// AUTHOR: Rob Tillaart
// DATE: 2023-01-25
// VERSION: 0.1.1
// PURPOSE: Arduino library for CD74HC4051 1x8 channel multiplexer and compatibles.
// VERSION: 0.2.0
// PURPOSE: Arduino library for CD74HC4051 1 x 8 channel multiplexer and compatibles.
// URL: https://github.com/RobTillaart/HC4051



#include "Arduino.h"

#define HC4051_LIB_VERSION (F("0.1.1"))
#define HC4051_LIB_VERSION (F("0.2.0"))


class HC4051
Expand Down Expand Up @@ -41,12 +41,24 @@ class HC4051

void setChannel(uint8_t channel)
{
if ((channel & 0x07) != _channel)
uint8_t _new = channel & 0x0F;
if (_new != _channel)
{
_channel = channel & 0x07;
digitalWrite(_pins[0], _channel & 0x01);
digitalWrite(_pins[1], _channel & 0x02);
digitalWrite(_pins[2], _channel & 0x04);
uint8_t _changed = _new ^ _channel;
uint8_t mask = 0x04;
uint8_t i = 2;
disable(); // prevent ghost channels.
while (mask)
{
// 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
45 changes: 35 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ multiplexer / demultiplexer and compatible devices.
The HC4051 allows e.g one analog port read up to 8 different analog channels,
or one digital port to read the state of 8 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 **A, B, C**

Expand All @@ -30,23 +34,25 @@ The device can be enabled/disabled by the enable line **INH**

#### Compatibles

to elaborate.
To elaborate.


#### Related to
#### Related

- https://github.com/RobTillaart/HC4051 (1x8 mux)
- https://github.com/RobTillaart/HC4052 (2x8 mux)
- 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 three **select pins** to four IO pins of your board.

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

Example multiplexing analog in.

Expand All @@ -69,6 +75,24 @@ 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 | pins | notes |
|:-----:|:-----:|:-----:|:------:|:-------:|
| IO | IO | IO | 0-7 | default usage
| IO | IO | GND | 0-3 |
| IO | 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 @@ -82,17 +106,18 @@ Set the three 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..7, this value is not checked, only the lower 3 bits will be used.
- **uint8_t getChannel()** get current channel 0..7.
- **uint8_t getChannel()** returns the current channel 0..7.
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 HC4051 to multiplex.
- **void disable()** disables the HC4051, 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
Expand Down
56 changes: 56 additions & 0 deletions examples/HC4051_performance/HC4051_performance.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// FILE: HC4051_performance.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for HC4051 8 channel (simple) multiplexer


#include "HC4051.h"

HC4051 mp(4, 5, 6, 8); // enable pin(8)

uint32_t start, stop;


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

mp.setChannel(10);

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

mp.setChannel(5);
delay(100);

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

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

}


void loop()
{
}


// -- END OF FILE --
15 changes: 15 additions & 0 deletions examples/HC4051_performance/output_0.2.0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

HC4051_performance.ino
HC4051 LIBRARY VERSION: 0.2.0

Channel 0: 20
Channel 1: 20
Channel 2: 24
Channel 3: 24
Channel 4: 28
Channel 5: 24
Channel 6: 24
Channel 7: 20

SetChannel: 4

2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/HC4051.git"
},
"version": "0.1.1",
"version": "0.2.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=HC4051
version=0.1.1
version=0.2.0
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library for a HC4051 1x8 channel multiplexer
Expand Down

0 comments on commit ec2586b

Please sign in to comment.