read and write from trrs connector

Hi, I am trying to write a TRRS entry to a computer, record the value by AUDACITY and then read the characters again from the program to the erduino through the TRRS connection and make sure that I got back the same entry I wrote.

This is my code:

void setup() 
{
   pinMode(1, OUTPUT);
}



void loop()
{
  for (int i = 0; i < 0xff+1; i++)
  {
    for (int x =0; x <1000; x++)
    {
      analogWrite(1, i);
    }
  }
}

The result looks like this software:

(see the good_sample.png attachmant)

When my inner loop becomes 10000, instead of getting the same shape only longer and longer (the logical thing) I get this shape:

(see the bad_sample.png attachmant)

This is the code with the different loop:

void setup() 
{
   pinMode(1, OUTPUT);
}



void loop()
{
  for (int i = 0; i < 0xff+1; i++)
  {
    for (int x =0; x <10000; x++)
    {
      analogWrite(1, i);
    }
  }
}

Another thing is strange, when the resulting form is narrow and there is background noises around the computer, it picks them up through the arduino (the arduino somehow becomes a microphone (even do there is NO mic connected to it) - I know it’s the arduino because when I disconnect the arduino from the TRRS leaving only the TRRS connected to the computer, the computer stop pick the noise up.

good_sample.PNG

Please read this:- How to use this forum

And also please read:- image guide

We don't like going off site for images especially for that spam riddled web site.

By the way what is a TRRS?

analogWrite(1, i);

Note this call only can take values of i of 0 to 255, anything over that wraps round to a value of modulus 255.

Grumpy_Mike: Please read this:- How to use this forum

And also please read:- image guide

Sorry the site didn't got my link as an image link from some reason.

Grumpy_Mike: By the way what is a TRRS?

TRRS is an audio jack that allow you to pass on the jack microphone and earphone and more (the standard audio jack of today phones (iphone/galaxy etc))

Grumpy_Mike: analogWrite(1, i);

Note this call only can take values of i of 0 to 255, anything over that wraps round to a value of modulus 255.

I know , in my code i not send over 255.

Which Arduino are you using? If you are using an UNO, pin 1 is not a PWM pin. (PWM: 3, 5, 6, 9, 10, and 11)

TRRS is an audio jack

OK, so in that context, what is a "TRRS entry"?

then read the characters again from the program

What characters? You are sending a PWM signal, not characters.

Post a wiring diagram of exactly how you've connected the Arduino through the TRRS to the PC.

Pete

Your biggest problem is that the “regular Arduino” doesn’t have a DAC (digital-to-analog converter) so there is no true analog output. analogWrite() is [u]PWM[/u] which can “act like” analog to control the speed of a motor or to make an LED appear dim.

The second problem is that there is no calibration. The Arduino puts-out 0V or 5V and the microphone input on the computer has a sensitivity somewhere in the ballpark of 10mV to 100mV.

if you had a true-analog signal and everything was calibrated, and if you had an 8-bit DAC, an 8-bit file in Audacity would be “close”, but since it’s analog it would never be perfect.

Plus, the DAC & ADC are not synchronized so the sampling is uncorrelated. For the same reason, if you digitize an analog recording twice you are not sampling the same-exact points in the analog waveform so you never get the same-exact bytes, even if you could eliminate analog noise and other “analog issues”, which you can’t.

You may be endangering your sound card or audio subsystem unless you ac-couple and attenuate the signal from the arduino to the computer. (Its usually microphone level)

You may be confusing any auto-microphone detect in the computer and its switching to its internal microphone as a result - an explanation for the background noise thing.

el_supremo:
Which Arduino are you using? If you are using an UNO, pin 1 is not a PWM pin. (PWM: 3, 5, 6, 9, 10, and 11)

Thank i made the change ( i used arduino uno and stm32 bluepill , and in the bluepill the PWM is 1 , in my Arduino UNO the PWM is 3)

el_supremo:
OK, so in that context, what is a “TRRS entry”?
What characters? You are sending a PWM signal, not characters.

TRRS entry = i cut trrs headphone and connected the mic in (white wire) to the pwm out and the mic ground(red and green around white wire) to Arduino ground.

I refferto the value i send with the AnalogWrite as a “Byte” because it’s have the boundry of 0<x<255
so I expecting to send let’s say 0x01 to Audacity in a loop and after that to be able to read the value 0x01 in loop (from the Audaciti after i reversed the wires and now the Arduino connected to the earphonr wires and not the mic)

el_supremo:
Post a wiring diagram of exactly how you’ve connected the Arduino through the TRRS to the PC.

I sorry i new to this , how can i draw this? there is some easy website to do it with it? or shukd i use the Paint software?

thank you!

DVDdoug: Your biggest problem is that the "regular Arduino" doesn't have a DAC (digital-to-analog converter) so there is no true analog output. analogWrite() is [u]PWM[/u] which can "act like" analog to control the speed of a motor or to make an LED appear dim.

Do you mean that it's impossible to use Arduino as DAC? if so are there any proper shield i can buy to do this job for the Arduino? My project is to take voice and scramble it, do outer board be more suited for the purpose ? (if so on wich board you recommended?)

DVDdoug: The second problem is that there is no calibration. The Arduino puts-out 0V or 5V and the microphone input on the computer has a sensitivity somewhere in the ballpark of 10mV to 100mV.

How the computer and normal headphone calibrate with each other?

DVDdoug: if you had a true-analog signal and everything was calibrated, and if you had an 8-bit DAC, an 8-bit file in Audacity would be "close", but since it's analog it would never be perfect.

If so how normal song pass throw a trrs connection to our day to day earphone? shouldn't we feel it's inconsistency?

DVDdoug: Plus, the DAC & ADC are not synchronized so the sampling is uncorrelated. For the same reason, if you digitize an analog recording twice you are not sampling the same-exact points in the analog waveform so you never get the same-exact bytes, even if you could eliminate analog noise and other "analog issues", which you can't.

How outer scramblers works? according to this they going to sound gibberish because they will get in the outer side different bytes from the scrambled byte sends to them?

Thank you very much for the detailed answer, I would appreciate if you could answer my follow-up questions also in detail

MarkT: You may be endangering your sound card or audio subsystem unless you ac-couple and attenuate the signal from the arduino to the computer. (Its usually microphone level)

Why ? I send the data to trrs that supposed to take this kind of data have I not?

MarkT: You may be confusing any auto-microphone detect in the computer and its switching to its internal microphone as a result - an explanation for the background noise thing.

Is there any protocol between the computer and the microphone and headphones? My understanding of speech simply activates an electric circuit on the side of the microphone but there is really nothing there that can create a coordination protocol for synchronizing the analog signal between the microphone and the computer

so I expecting to send let's say 0x01 to Audacity in a loop and after that to be able to read the value 0x01 in loop

No that is not what happens.

You send a 0x01 to an analogue write and it produces a square wave of 0 to 5V with it being high for 1/256 th of a cycle and low for 255/256 ths of a cycle. You will never read that back as 01 from anything.

How outer scramblers works?

Simple world war 2 scramblers work by inverting all the frequencies in the analogue audio. This is done by modulating the signal, and choosing the upper side band and decoding it as if it were the lower sideband.

I don’t think you understand how audio and in particular digital audio works.

Grumpy_Mike: No that is not what happens.

You send a 0x01 to an analogue write and it produces a square wave of 0 to 5V with it being high for 1/256 th of a cycle and low for 255/256 ths of a cycle. You will never read that back as 01 from anything.

So what I would get when I will try to read it?

Grumpy_Mike: I don’t think you understand how audio and in particular digital audio works.

You are probably right, however there must be some component(shield/chip/breakout) or algorithm for converting the analog signals to digital signals in a reasonable way, otherwise VOIP for that matter would not work at all ... So i want to know how this is done properly? (or what i need to make it work properly)

Thank you for your detailed reply

So what I would get when I will try to read it?

You could get any number from 0 to 255 depending on the synchronization between the sample rate and the PWM. The odds are you will get zero more times than not.

So i want to know how this is done properly?

Basically you need an analogue to digital converter A/D, to sample the audio at regular intervals. The number of times a second you take a reading sets the highest frequency you can reproduce. This will be half the sample rate. So if you sample at 10000 times a second you will be able to reproduce frequencies of up to 5000 Hz or 5 KHz.

The actual number of bits produced by the A/D determines the quality, with 8 bits being telephone quality and 16 bits being CD quality.

The maximum number the A/D can measure represents the maximum peak audio voltage. Audio waveforms are symmetrical about a reference voltage. In analogue this is almost always 0V, it the digital world this is normally half the range of the A/D as most A/D converts do not cope with negitave voltages. So the analogue audio signal has to be shifted up or as we say biased to some voltage value, normally 2.5V for an Arduino.

In the Arduino world most A/D converters are 10 bits and can be read at just under 10000 times a second. An Arduino has no D/A digital to analogue converter so you either need an external one or use high speed PWM ( about 40KHz ) and a filter to approximate an 8 bit D/A.

You can fit both an external A/D and D/A to an Arduino for better results.

As data is produces so fast and there is little free storage memory in an Arduino so the number of things you can do with that audio data is limited. But you could again fit an external memory to act as a buffer.

This is just a quick overview for lots more information google arround or read my book. https://www.apress.com/gb/book/9781484217207