Customize Knob_LightLevev_x2_FMSynth with others sensors

84 views
Skip to first unread message

Sal Mec

unread,
Jul 15, 2024, 3:50:31 PM7/15/24
to Mozzi-users
Dear Community,
I would like to customize Knob_LightLevev_x2_FMSynth adding a distance sensor (VL53LXX-V2) replacing the frequency selection by KNOB_PIN with the distance measured by the sensor.
I'm able to run the original example on my Arduino UNO clone and I can hear a good quality sound, but when I add the sensor I can hear some rithmic noise.
I have read about refresh rate issue and I have choosen 128 Hz (I've also tried 256 and 64Hz) but without success.
I've also commented all Serial.print and choose all different speed on Serial communication from 9600 up to 115200 but the noise is still present and is not affected by serial speed.
I normally power the Arduino though USB, but I've tryed also a different power switching and a new 9V battery, with the same results.
How can I avoid the noise issue?
below the code (the added code to the original exampled are marked by comments):
//CODE
#define MOZZI_ANALOG_READ_RESOLUTION 10  // code below assumes readings to be in the classic 10-bit (0-1023) range
#include <Mozzi.h>
#include <Oscil.h>                // oscillator
#include <tables/cos2048_int8.h>  // table for Oscils to play
#include <Smooth.h>
#include <AutoMap.h>  // maps unpredictable inputs to a range

// desired carrier frequency max and min, for AutoMap
const int MIN_CARRIER_FREQ = 22;
const int MAX_CARRIER_FREQ = 440;

// desired intensity max and min, for AutoMap, note they're inverted for reverse dynamics
const int MIN_INTENSITY = 700;
const int MAX_INTENSITY = 10;

// desired mod speed max and min, for AutoMap, note they're inverted for reverse dynamics
const int MIN_MOD_SPEED = 10000;
const int MAX_MOD_SPEED = 1;

AutoMap kMapCarrierFreq(0, 1023, MIN_CARRIER_FREQ, MAX_CARRIER_FREQ);
AutoMap kMapIntensity(0, 1023, MIN_INTENSITY, MAX_INTENSITY);
AutoMap kMapModSpeed(0, 1023, MIN_MOD_SPEED, MAX_MOD_SPEED);

const int KNOB_PIN = 0;  // set the input for the knob to analog pin 0
const int LDR1_PIN = 1;  // set the analog input for fm_intensity to pin 1
const int LDR2_PIN = 2;  // set the analog input for mod rate to pin 2

Oscil<COS2048_NUM_CELLS, MOZZI_AUDIO_RATE> aCarrier(COS2048_DATA);
Oscil<COS2048_NUM_CELLS, MOZZI_AUDIO_RATE> aModulator(COS2048_DATA);
Oscil<COS2048_NUM_CELLS, MOZZI_CONTROL_RATE> kIntensityMod(COS2048_DATA);

int mod_ratio = 5;  // brightness (harmonics)
long fm_intensity;  // carries control info from updateControl to updateAudio

// smoothing for intensity to remove clicks on transitions
float smoothness = 0.95f;
Smooth<long> aSmoothIntensity(smoothness);
#define CONTROL_RATE 128 // Hz, powers of 2 are most reliable

//ADD SENSOR
#include <Wire.h>
#include <VL53L0X.h>

VL53L0X sensor;
// END ADD SENSOR



void setup() {
  //Serial.begin(9600); // for Teensy 3.1, beware printout can cause glitches
  Serial.begin(115200);  // set up the Serial output so we can look at the piezo values // set up the Serial output so we can look at the light level
  startMozzi(CONTROL_RATE);          // :))

  //ADD SENSOR
  Wire.begin();
  sensor.init();
  sensor.setTimeout(500);
    // Start continuous back-to-back mode (take readings as
    // fast as possible).  To use continuous timed mode
    // instead, provide a desired inter-measurement period in
    // ms (e.g. sensor.startContinuous(100)).
    sensor.startContinuous();
  //END ADD SENSOR
}


void updateControl() {
  // read the knob
  int knob_value = mozziAnalogRead(KNOB_PIN);  // value is 0-1023


  // map the knob to carrier frequency
  //  int carrier_freq = kMapCarrierFreq(knob_value);
  int carrier_freq = sensor.readRangeContinuousMillimeters();
  //calculate the modulation frequency to stay in ratio
  int mod_freq = carrier_freq * mod_ratio;

  // set the FM oscillator frequencies
  aCarrier.setFreq(carrier_freq);
  aModulator.setFreq(mod_freq);

  // read the light dependent resistor on the width Analog input pin
  int LDR1_value = mozziAnalogRead(LDR1_PIN);  // value is 0-1023
  // print the value to the Serial monitor for debugging
 /* Serial.print("LDR1 = ");
  Serial.print(LDR1_value);
  Serial.print("\t");  // prints a tab*/
 
  int LDR1_calibrated = kMapIntensity(LDR1_value);
 /* Serial.print("LDR1_calibrated = ");
  Serial.print(LDR1_calibrated);
  Serial.print("\t");  // prints a tab*/

  // calculate the fm_intensity
  fm_intensity = ((long)LDR1_calibrated * (kIntensityMod.next() + 128)) >> 8;  // shift back to range after 8 bit multiply
 /* Serial.print("fm_intensity = ");
  Serial.print(fm_intensity);
  Serial.print("\t");  // prints a tab*/

  // read the light dependent resistor on the speed Analog input pin
  int LDR2_value = mozziAnalogRead(LDR2_PIN);  // value is 0-1023
/*  Serial.print("LDR2 = ");
  Serial.print(LDR2_value);
  Serial.print("\t");  // prints a tab*/

  // use a float here for low frequencies
  float mod_speed = (float)kMapModSpeed(LDR2_value) / 1000;
 /* Serial.print("   mod_speed = ");
  Serial.print(mod_speed);
  kIntensityMod.setFreq(mod_speed);

  Serial.println();  // finally, print a carraige return for the next line of debugging info*/
}


AudioOutput updateAudio() {
  long modulation = aSmoothIntensity.next(fm_intensity) * aModulator.next();
  return MonoOutput::from8Bit(aCarrier.phMod(modulation));
}


void loop() {
  audioHook();
}

//END CODE

Thanks in advance

Salmec

tomco...@live.fr

unread,
Jul 19, 2024, 10:18:19 AM7/19/24
to Mozzi-users
Hi,
Sorry for the delay.

Does this also happens with the sensor connected (and updated) but if you are not using the data from the sensor for the synthesis?

If no, this seems to mean that the data from the sensor is noisy (picking up the mains or something). One way to solve that in soft would be to Smooth or LowPass the sensor output. In hardware, it would be interesting for you to have a look at the actual output of the sensor, for instance with a scope if you have one, to have an idea of the noise.

Hope this helps,
Tom

Mozzi-users

unread,
Jul 19, 2024, 8:33:39 PM7/19/24
to Mozzi-users
It's possible that the I2C communication with the VL53LXX-V2 sensor is blocking the audio - the Mozzi example 11.Communication>2Wire_Read_ADXL345 shows a way to work around it if that's the problem...
If working out the nonblocking code is too time-consuming and you don't need that particular sensor (VL53LXX-V2), the HC-SR04 ultrasonic distance sensor is easy to use (but also requires a fairly simple workaround in Mozzi).  I can post a sketch if you go that way.

Sal Mec

unread,
Jul 22, 2024, 11:14:27 AM7/22/24
to Mozzi-users
Dear Tom,
thanks for your interest and suggestion, I've made some test in order to understand better the situation:

1) deleting all sensor reference, the sound comes with a good quality the frequency selection is done by A0 analogread 
  int knob_value = mozziAnalogRead(KNOB_PIN);
int carrier_freq = kMapCarrierFreq(knob_value);
2) in the setup I've initialized the sensor
  Wire.begin();
  sensor.init();
  sensor.setTimeout(500);
  sensor.startContinuous(CONTROL_RATE);
but data are not taken from the sensor
 //int carrier_freq = sensor.readRangeContinuousMillimeters(); is still commented
the audio quality is good.

3) if I take data from sensor
 int carrier_freq = sensor.readRangeContinuousMillimeters();
the audio output is affected by noise...
I've tried also to change sensor.startContinuous(CONTROL_RATE);
with sensor.startContinuous(64); without any upgrade...

any other suggestion?

Regarding HC-SR04, I can test it so a working example is highly appreciated 

thanks a lot

Salmec

Mozzi-users

unread,
Jul 22, 2024, 8:16:10 PM7/22/24
to Mozzi-users
Here's the hc-sr04 example..
Ultrasonic_Distance_Sinewave.ino

Sal Mec

unread,
Jul 24, 2024, 2:35:06 AM7/24/24
to Mozzi-users
Thanks,
I'll try with HC-SR04 sensor, but I would like to implement Laser distance with IC2 communication, any other suggestion or troubleshoting?

Thanks in advance 

Salmec

Sal Mec

unread,
Sep 5, 2024, 6:15:13 AM9/5/24
to Mozzi-users
Dear Community,
I have made HC-SR04 working with Mozzi (thanks a lot), but now I'm struggling with Display, I would like to show the note (frequency) on a display and I've used:
- IC2 LCD 16x2 display, but noise cominq from IC2 communication degrade a lot sound quality (the same issue that i found with the use of distance sensor VL53LXX-V2 )
- Matrix LED display MAX7219 but I have glitches on display changes, I've found the solution here (https://forum.arduino.cc/t/arduino-uno-max7219-noise-in-audio-output/1111317/3) and I'm waiting to receive hardware in order to check.

Meanwhile I would like to use two Arduinos:
- The first one to use the distance sensor and generate the tone frequency (with PWM functionality see : https://code.google.com/archive/p/tinkerit/wikis/Auduino.wiki )
- The second one take as input the tone generated and with mozzi I could modify the waveform acting as a syntesizer with analogue input.

I've discovered that Analogue input are disabled, but all the related posts refers to an old version of mozzi library where is indicated to change the configuration  file mozzi_config.h where it says:

    #define USE_AUDIO_INPUT false

 mozzi_config.h  do not exixt but I found that MOZZI_AUDIO_INPUT_PIN is present in some .h file but I do not understand how to abilitate it, please indicate how to do...

Moreover in some post (https://groups.google.com/g/mozzi-users/c/rP2AaI5Yi-M) is written:  When audio input is used, don't read other analog channels as they'll interfere with the audio sampling.

Does it means that if I sample audio input I cannot use other analogue inputs? For example to read potentiometer values?

If the last sentence is true I have to change and find another approach.

Thanks in advance.

Kind Regards

salmec

Mozzi-users

unread,
Sep 5, 2024, 8:17:16 AM9/5/24
to Mozzi-users

Hi salmec,
not an answer to everything here, but have you checked the current version's Audio Input examples?  I'm not set up to test them myself at the moment but they show audio and other analog inputs in use together... They are configured in the sketch in the current version (v2), so you don't need to alter a config file any more.  Let us know if they're not working!

Mozzi-users

unread,
Sep 5, 2024, 8:25:06 AM9/5/24
to Mozzi-users
If you change your mind about the display there's a good chance you can solve the IC2 blockages, if you spend a few hours in programming hell.  I've done it a few times for different modules and as long as they have predictable and not enormous data transfers, the problems can be programmed away following the technique in Mozzi example 11.Communication>TwoWire_Read_ADXL345...

Sal Mec

unread,
Sep 5, 2024, 8:55:19 AM9/5/24
to Mozzi-users
Dear,
thanks for your quick answer, I wrote here because I cannot compile the audio input examples.
I had installed 2.0 Version of mozzi library, and I've updated at 2.0.1, NOW I can compile, and I can hear sound...

thanks

Salmec

Sal Mec

unread,
Sep 5, 2024, 8:58:12 AM9/5/24
to Mozzi-users
I try the 2 arduino appoach but I'll try also  TwoWire_Read_ADXL345... the point is that I do not want to fall in programming hell ;)

maybe  could you have a dummy example code working?

thanks in advance.

Salmec

Reply all
Reply to author
Forward
0 new messages