Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15317

I need help with Arduino UNO project connecting Ultrasonic sensor and a LED matrix

$
0
0

Hello Guys,
I'm working on an Arduino project that includes an ultrasonic sensor (HC-SR04) and an LED matrix (MAX7219). I am trying to light up the rows of LED matrix by varying the ultrasonic range.

from my code, varying the ultrasonic range from 0 to 400 cm, I can able to light up the matrix from 0 to 7 rows but not vice versa (400 to 0 cm, unable to light off the LED rows).

Here I have attached the code.

#include "LedControl.h"
#include "binary.h"
/* DIN connects to pin 12 CLK connects to pin 11 CS connects to pin 10 */
LedControl lc=LedControl(12,11,10,1);
#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04 // defines variables
int duration; // variable for the duration of sound wave travel
int distance;//variable for the distance measurement
byte dot[8]= {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

void setup()
{
	lc.shutdown(0,false); //sets up 8x8 matrix
	lc.setIntensity(0,16); // Set brightness to a medium value
	lc.clearDisplay(0); // Clear the display
	pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
	pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
}

void printByte(byte character [])
{
  int i = 0;
  for(i=0;i<8;i++)
	{
    lc.setRow(0,i,character[i]);
  }
}

void loop()
{
	digitalWrite(trigPin, LOW);
	delayMicroseconds(2);// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
	digitalWrite(trigPin, HIGH);
	delayMicroseconds(10); //Creates a ten microsecond delay
	digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds

	duration = pulseIn(echoPin, HIGH);// Calculating the distance
	distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)

	int scale = map(distance, 0 , 400, 0, 8);  // Mapping 0 to 400CM with 0 to 7 rows
	for (int i=0;i<scale;i++)
		{
      dot[i] = 0xff;
    }
	printByte(dot);
	delay(100);
}

here is the json for circuit connection: just copy and paste it in diagram.json tab in wokwi.com,

{
  "version": 1,
  "author": "Uri Shaked",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-uno", "id": "uno", "top": 115.8, "left": -115.8, "attrs": {} },
    {
      "type": "wokwi-hc-sr04",
      "id": "ultrasonic",
      "top": 174.3,
      "left": 283.9,
      "attrs": { "distance": "400" }
    },
    {
      "type": "wokwi-max7219-matrix",
      "id": "matrix1",
      "top": -66.6,
      "left": -78.96,
      "attrs": { "chain": "4" }
    }
  ],
  "connections": [
    [ "uno:GND.3", "ultrasonic:GND", "black", [ "v30", "h120", "*" ] ],
    [ "uno:2", "ultrasonic:ECHO", "green", [ "v-20", "*", "h-120", "v20" ] ],
    [ "uno:3", "ultrasonic:TRIG", "purple", [ "v-24", "*", "h-108", "v16" ] ],
    [ "uno:5V", "ultrasonic:VCC", "red", [ "v18", "h120", "*" ] ],
    [ "matrix1:V+", "uno:VIN", "green", [ "h240", "v403.2", "h-422.4" ] ],
    [ "matrix1:GND", "uno:GND.3", "black", [ "h259.2", "v403.2", "h-460.8" ] ],
    [ "matrix1:DIN", "uno:12", "cyan", [ "h96", "v96", "h-336" ] ],
    [ "matrix1:CLK", "uno:11", "blue", [ "h48", "v48", "h-278.4" ] ],
    [ "matrix1:CS", "uno:10", "magenta", [ "h76.8", "v67.2", "h-297.6" ] ]
  ],
  "dependencies": {}
}

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 15317

Trending Articles