Showing posts with label ping. Show all posts
Showing posts with label ping. Show all posts

Wednesday, June 03, 2020

Teensy 3.6 with HC-SR04 Distance Sensor

The HC-SR04 is a low-cost ultrasonic sensor that uses two transducers to measure the distance in front before being blocked by an object or surface. The sensor has four pins GND, VCC, TRIG and ECHO.



Hardware Setup

  • HC-SR04 Ground should be connected to Teensy 3.6 ground. 
  • HC-SR04 VCC should be connected to Vin (which should output 5V if connected to USB). 
  • HC-SR04 TRIG should be connected to Teensy 3.6 digital pin 0. 
  • HC-SR04 ECHO should be connected to one leg of a resistor (1k to 100k). The other leg of the resistor should be connected to Teensy 3.6 digital pin 1. The other leg of the resistor should also be connected to one leg of a second resistor (1k to 100k - same value as the first resistor). The other leg of the second resistor should be connected to ground. 
  • These resistors going to ground and pin 1 form a voltage divider that takes the output voltage from the HC-SR04 from 5V to 2.5V, which is suitable for the Teensy 3.6. 
Here is a photo and layout for the breadboard with and without the sensor connected. Note the connections for the resistors, ground, and digital pins. 









Example 1 - Serial Output

These examples use the HCSR04 library by Martin Sosic, which can be downloaded via the Library Manager tool in Arduino. 

The Arduino Serial monitor will output the measurement as a distance in cm. 






Example 2 - USB MIDI CC

The distance sensor is scaled to a suitable range of 0 - 127 and sent over USB as MIDI CC values, that can be used in Ableton Live and other software to control sound and music parameters.


View the code here: https://github.com/little-scale/arduino-sketches/blob/master/Distance_Sensor_HCSR04_2_USB_MIDI_CC.ino



Example 3 - USB MIDI Pitch with Note On via Push Button

A button is added to the setup, connected to Teensy 3.6 digital pin 32. Pressing the button generates a note on event that uses the distance sensor to set the pitch of the note. Depressing the button generates a note off event.





View the code here: https://github.com/little-scale/arduino-sketches/blob/master/Distance_Sensor_HCSR04_3_USB_MIDI_Pitch.ino



Saturday, March 01, 2014

Multiple HC-SR04 Sensors on One Arduino Board



HC -SR - 04 trig pins connect to Arduino pins 2, 4, 6, 8, 10, 12.

HC -SR - 04 echo pins connect to Arduino pins 3, 5, 7, 9, 11, 13.




Adjust this line:


  for(int i = 0; i < 6; i ++) {

to change the number of sensors to less than six.




Data is sent as MIDI formatted high resolution pitch bend data. Every new ping sensor has a dedicated channel in this way. The resolution is up to 14 bit.




Arduino Code:
 
 
#define led 11
#define led2 10
unsigned int j; 
byte flag = 0; 

void setup() {
  Serial.begin (57600);
  for(int i = 0; i < 6; i ++) {
    pinMode((i * 2) + 2, OUTPUT); 
    pinMode((i * 2) + 3, INPUT); 
  }

  // pinMode(led, OUTPUT);
  // pinMode(led2, OUTPUT);
}

void loop() {

  for(int i = 0; i < 6; i ++) {
    readHCSR04((i * 2) + 2, (i * 2) + 3, i + 1);
  }

  delay(100); 

}

void readHCSR04(int trigPin, int echoPin, int sensorNumber) {
  long duration, distance;
  byte working; 
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
  // delayMicroseconds(1000); // - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH, 100000);
    

    Serial.write(0xE0 + sensorNumber); 
    working = duration & 0x7F; 
    Serial.write(working); 
    working = (duration >> 7) & 0x7F;
    Serial.write(working);
    
    
 //   delay(10); 
  
}



Friday, February 28, 2014

Multiple Ping Sensors on One Arduino Board




Ping data pins connected to Arduino 2, 4, 6, 8 etc for each sensor



Adjust this line:

  for(int i = 0; i < 1; i ++) {

to increase the number of sensors beyond one sensor.




Data is sent as MIDI formatted high resolution pitch bend data. Every new ping sensor has a dedicated channel in this way. The resolution is up to 14 bit.




Arduino Code:

#define led 11
#define led2 10
unsigned int j; 
byte flag = 0; 

void setup() {
  Serial.begin (57600);

  // pinMode(led, OUTPUT);
  // pinMode(led2, OUTPUT);
}

void loop() {

  for(int i = 0; i < 1; i ++) {
    readHCSR04((i * 2) + 2, (i * 2) + 3, i + 1);
  }

  delay(100); 

}

void readHCSR04(int trigPin, int echoPin, int sensorNumber) {
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;
  byte working; 

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(trigPin, INPUT);
  duration = pulseIn(trigPin, HIGH);

  Serial.write(0xE0 + sensorNumber); 
  working = duration & 0x7F; 
  Serial.write(working); 
  working = (duration >> 7) & 0x7F;
  Serial.write(working);

}




Sunday, September 15, 2013

Ping-Based YES NO Swipe Interface



I made a simple YES / NO swipe interface using a Teensy and two Ping distance sensors.

The ping signal pin is connected to pins 7 and 8 (for the YES and NO Pings respectively).

There are values in the code that set the min, max and time-based parameters for when a swipe is recognised as being a swipe.

They are:

long distance_min = 800; // change to set minimum swipe range distance
long distance_max = 2200; // change to set maximum swipe range distance
int swipe_reactivity = 2; // change to set swipe reactivity in time
int swipe_delay = 1000; // change to set delay after positive swipe is read

The YES swipe is transmitted by a Serial port message at 9600 baud saying "swipe yes", as well as a USB MIDI control change message of channel 1, controller 1 and value 127.

The YES swipe is transmitted by a Serial port message at 9600 baud saying "swipe no", as well as a USB MIDI control change message of channel 1, controller 2 and value 127.

An example Max patch which differentiates these values is as follows:



Download the Arduino Teensy code here: http://milkcrate.com.au/_other/useless/swiper.zip

Saturday, April 26, 2008

Ping Sans Max

This is a follow-on from the previous post, and shows how to send PING))) ultrasonic sensor data via MIDI continuous controller messages, avoiding the use of Max/MSP or any other intermediate software layer. Of course, this requires a MIDI to USB or similar hardware interface.

The Arduino board and PING))) sensor still require a power source. This can be via the USB cable (as in this example) or potentially a mains AC/DC transformer or a suitable battery.

Hardware Connections

You will need
• 1 x PING))) sensor
• 1 x Arduino board
• A breadboard
• Some jumper wires
• A USB A to B cable
• A 220Ω resistor
• Three alligator clips
• A 5 pin DIN connector

Please note that an older NG board was used in this demonstration, but a Diecimilia board etc. should of course be fine as well.


Breadboarding the Circuit

1. Here we have the basic breadboard set up next to the Arduino. Note the way that the breadboard is structured - the vertical blue and red columns are electrically linked as long, single lines, and the horizontal half-rows of five points are electrically linked.



2. Place the Ping))) sensor onto the breadboard. Note that the sensor has three pins. When looking at the front of the sensor, these three pins are marked GND (ground), 5V (the power supply) and SIG (the data signal pin).



3. Set up the power bussing for the breadboard. Make a connection between the 5V pin on the Arduino board and the red vertical line. The red vertical line is now the supply voltage bus. Make a connection between the GND pin on the Arduino board and the blue vertical line. The blue vertical line is now the ground bus.



4. Connect the data signal (marked SIG) pin of the PING))) sensor to digital pin 7 of the Arduino board.



5. Connect the power supply pin (marked 5V) of the PING))) sensor to the red vertical line (the power supply bus).



6. Connect the ground pin (marked GND) of the PING))) sensor to the blue vertical line (the ground bus).



7. Connect one of the 220Ω resistor to the red vertical line (the power supply bus). The 220Ω resistor used in this example had a rating of 5% and 0.25W. However, other resistors with different ratings (for example 2% or 1% tolerance etc) are suitable.



8. Connect one end of an alligator lead (red) to pin 4 of the 5 pin DIN connector.




8. Connect the other end of the alligator lead (red) to the other leg of the 220Ω resistor.



9. Connect one end of an alligator lead (yellow) to pin 5 of the 5 pin DIN connector.



10. Connect the other end of the alligator lead (yellow) to the TX pin (digital pin 1) of the Arduino board).



11. Connect one end of an alligator lead (green) to pin 2 of the 5 pin DIN connector.



12. Connect the other end of the alligator lead (green) to the blue vertical line (the ground bus).




Software Setup

Upload the sketch to the Arduino board
1) Download the code from here:
http://milkcrate.com.au/_other/downloads/ping/MIDI_output/

2) Open the Arduino software. Version 010 was used for this demonstration.

3) Paste the code into a new sketch.

4) Verify the code by going to Sketch > Verify.

5) Make sure the Arduino board is connected to the computer.

6) Select the correct serial port by going to Tools > Serial Port. The Arduino should come up as /dev/tty.usbserial-A4---

7) Upload the sketch by going to File > Upload to I/O Board. Depending on the type of Arduino board you have, you might need to physically press the reset button while you execute this command.

8) The RX and TX LED's on the Arduino board should flash, indicating the upload process.


Editing the Arduino Code (if Needed)
The three most important variables are the first three defined in the sketch, namely:
• delay_time (int)
• MIDI_channel (byte)
• cc_number (byte)

delay_time sets the length of delay (in ms) between successive read/write cycles. The default is 40 ms. MIDI_channel is the channel (1 - 16) to send the MIDI data on. cc_number is the MIDI continuous controller number (0 - 127) to send the MIDI data on. The default MIDI setup is MIDI channel 1 and controller number 127.

You may wish to edit these values to suite your purposes.


Read the MIDI Data
Setup your environment to read MIDI continuous controller messages on the interface, channel and cc number as dictated by your setup. The closer an object is to the PING))), the lower the MIDI cc data.

Like so...

Thursday, April 24, 2008

Ping Time

Here is a very basic setup for how to connect a Ping))) ultrasonic sensor to a host computer and receive incoming data, converting it to MIDI continuous control messages. In fact, this is probably the simplest I can think of right now in order to get data from Ping))) to elsewhere.

The response of the sensor is so-so, and sometimes I seem to get weird data readings with mine. However, this could either be a) some of the maths scaling that I have added to the Arduino code or b) something in my environment (which is quite cluttered) could be interfering with the sensing mechanism.

Hardware Connections

You will need
• 1 x PING))) sensor
• 1 x Arduino board
• A breadboard
• Some jumper wires
• A USB A to B cable

Please note that an older NG board was used in this demonstration, but a Diecimilia board etc. should of course be fine as well.


Breadboarding the Circuit

1. Here we have the basic breadboard set up next to the Arduino. Note the way that the breadboard is structured - the vertical blue and red columns are electrically linked as long, single lines, and the horizontal half-rows of five points are electrically linked.



2. Place the Ping))) sensor onto the breadboard. Note that the sensor has three pins. When looking at the front of the sensor, these three pins are marked GND (ground), 5V (the power supply) and SIG (the data signal pin).



3. Set up the power bussing for the breadboard. Make a connection between the 5V pin on the Arduino board and the red vertical line. The red vertical line is now the supply voltage bus. Make a connection between the GND pin on the Arduino board and the blue vertical line. The blue vertical line is now the ground bus.



4. Connect the data signal (marked SIG) pin of the PING))) sensor to digital pin 7 of the Arduino board.



5. Connect the power supply pin (marked 5V) of the PING))) sensor to the red vertical line (the power supply bus).



6. Connect the ground pin (marked GND) of the PING))) sensor to the blue vertical line (the ground bus).





Software Setup

The software setup is made up of three main parts:

A) Upload the sketch to the Arduino board (this only has to be done once).
B) Open and set up the Max/MSP patch.
C) Connect to your favourite electronic music making / synthesis environment (SuperCollider, Max/MSP, Ableton Live etc).



A) Upload the sketch to the Arduino board.

1) Download the code from here:
http://milkcrate.com.au/_other/downloads/ping/very_simple/

2) Open the Arduino software. Version 010 was used for this demonstration.

3) Paste the code into a new sketch.

4) Verify the code by going to Sketch > Verify.

5) Make sure the Arduino board is connected to the computer.

6) Select the correct serial port by going to Tools > Serial Port. The Arduino should come up as /dev/tty.usbserial-A4---

7) Upload the sketch by going to File > Upload to I/O Board. Depending on the type of Arduino board you have, you might need to physically press the reset button while you execute this command.

8) The RX and TX LED's on the Arduino board should flash, indicating the upload process.



B) Open and set up the Max/MSP patch.


1) Make sure the Ping))) circuit is set up and that the Arduino board is plugged into the host computer.

2) Open up the Max/MSP patch in Max/MSP. The patch can be downloaded here:
http://milkcrate.com.au/_other/downloads/ping/very_simple/

3) You may need to adjust the first argument of the serial object, depending on which serial port your Arduino board connects to your computer. Experiment or use a print message connected to the serial object in order to work out which symbol is which serial port.

4) You may wish to adjust the continuous controller number (the argument of the ctlout object).



C) Connect to your favourite environment
By sending the control data along a virtual MIDI path, you can now send the Ping))) data to may other types of places and control other things.