Arduino and Multiplexer

74HC4051 Multiplixer  and 8 LED.

list of products from Amazon (Not mine, i just link them!) Scroll Down For Article! Note: Elegoo boards are not Arduino, but they are compatible with Arduino IDE and are mutch more affordable!


Hello In this article we will see how you can increase the output of the Arduino UNO board by using the  74HC4051 Multiplixer , we wil controll 8 LEDS! This Project is simulated using Proteus 8!
Schematic:
74HC4051 Multiplixer  and 8 LED


 

Code for LED Brightness Controller:

// File: led_brightness_controller_with_inh.ino

// Define multiplexer control pins
#define A 2
#define B 3
#define C 4

// Define PWM output pin
#define PWM_PIN 9

// Define INH (inhibit) pin
#define INH 5

// Brightness levels for each LED (0 to 255)
int brightnessLevels[8] = {50, 100, 150, 200, 255, 180, 90, 60};

void setup() {
  // Set control pins as output
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);

  // Set INH pin as output and disable inhibit
  pinMode(INH, OUTPUT);
  digitalWrite(INH, LOW); // Enable multiplexer

  // Set PWM pin as output
  pinMode(PWM_PIN, OUTPUT);
}

void loop() {
  for (int i = 0; i < 8; i++) {
    // Set multiplexer channel
    selectChannel(i);

    // Write the corresponding brightness level to the PWM pin
    analogWrite(PWM_PIN, brightnessLevels[i]);

    // Hold for a short time to make the brightness visible
    delay(100);
  }
}

// Function to select the active multiplexer channel
void selectChannel(int channel) {
  digitalWrite(A, channel & 0x01); // Least significant bit
  digitalWrite(B, (channel >> 1) & 0x01);
  digitalWrite(C, (channel >> 2) & 0x01);
}


Code: Brightness Equalizer


#define A 2
#define B 3
#define C 4
#define PWM_PIN 9
#define INH 5
#define POT_PIN A0

void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(INH, OUTPUT);
  digitalWrite(INH, LOW); // Enable multiplexer
  pinMode(PWM_PIN, OUTPUT);
}

void loop() {
  int potValue = analogRead(POT_PIN);         // Read potentiometer value
  int brightness = map(potValue, 0, 1023, 0, 255); // Map to PWM range

  for (int i = 0; i < 8; i++) {
    selectChannel(i);
    analogWrite(PWM_PIN, brightness);
    delay(100);
  }
}

void selectChannel(int channel) {
  digitalWrite(A, channel & 0x01);
  digitalWrite(B, (channel >> 1) & 0x01);
  digitalWrite(C, (channel >> 2) & 0x01);
}

Code: SOS Signal

#define A 2
#define B 3
#define C 4
#define PWM_PIN 9
#define INH 5

void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(INH, OUTPUT);
  digitalWrite(INH, LOW); // Enable multiplexer
  pinMode(PWM_PIN, OUTPUT);
}

void loop() {
  for (int i = 0; i < 8; i++) {
    sendSOS(i);
    delay(2000); // Pause between SOS signals
  }
}

void sendSOS(int channel) {
  selectChannel(channel);

  // Dot-Dot-Dot
  for (int i = 0; i < 3; i++) {
    analogWrite(PWM_PIN, 255);
    delay(200); // Short flash
    analogWrite(PWM_PIN, 0);
    delay(200);
  }

  // Dash-Dash-Dash
  for (int i = 0; i < 3; i++) {
    analogWrite(PWM_PIN, 255);
    delay(600); // Long flash
    analogWrite(PWM_PIN, 0);
    delay(200);
  }

  // Dot-Dot-Dot
  for (int i = 0; i < 3; i++) {
    analogWrite(PWM_PIN, 255);
    delay(200); // Short flash
    analogWrite(PWM_PIN, 0);
    delay(200);
  }
}

void selectChannel(int channel) {
  digitalWrite(A, channel & 0x01);
  digitalWrite(B, (channel >> 1) & 0x01);
  digitalWrite(C, (channel >> 2) & 0x01);
}

Code: LED Chaser

// File: led_chaser.ino

#define A 2
#define B 3
#define C 4
#define PWM_PIN 9
#define INH 5

void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(INH, OUTPUT);
  digitalWrite(INH, LOW); // Enable multiplexer
  pinMode(PWM_PIN, OUTPUT);
}

void loop() {
  for (int i = 0; i < 8; i++) {
    // Gradually increase brightness
    for (int brightness = 0; brightness <= 255; brightness += 5) {
      selectChannel(i);
      analogWrite(PWM_PIN, brightness);
      delay(10);
    }

    // Gradually decrease brightness
    for (int brightness = 255; brightness >= 0; brightness -= 5) {
      selectChannel(i);
      analogWrite(PWM_PIN, brightness);
      delay(10);
    }
  }
}

void selectChannel(int channel) {
  digitalWrite(A, channel & 0x01);
  digitalWrite(B, (channel >> 1) & 0x01);
  digitalWrite(C, (channel >> 2) & 0x01);
}

Schematic of this project:



Reaction game




Code: Reaction Game

#include <Arduino.h>

#define A 2
#define B 3
#define C 4
#define PWM_PIN 9
#define INH 5
#define BUTTON_PIN 6

void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(INH, OUTPUT);
  digitalWrite(INH, LOW); // Enable multiplexer
  pinMode(PWM_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP); // Button with pull-up resistor
  Serial.begin(9600);
}

void loop() {
  int randomLED = random(0, 8); // Pick a random LED
  selectChannel(randomLED);
  analogWrite(PWM_PIN, 255); // Turn on LED

  unsigned long startTime = millis();
  while (digitalRead(BUTTON_PIN) == HIGH); // Wait for button press

  unsigned long reactionTime = millis() - startTime;
  Serial.print("Reaction Time: ");
  Serial.println(reactionTime);

  analogWrite(PWM_PIN, 0); // Turn off LED
  delay(1000); // Wait before next round
}

void selectChannel(int channel) {
  digitalWrite(A, channel & 0x01);
  digitalWrite(B, (channel >> 1) & 0x01);
  digitalWrite(C, (channel >> 2) & 0x01);
}




Find the full Project here:
https://drive.google.com/file/d/1LNoW0uWOdzJTwrX-_cId1JDb2VolpdRl/view?usp=drive_link



Post a Comment

0 Comments