6 Replies Latest reply on Jan 11, 2020 12:39 PM by aspork42

    reproducing sine wave accurately


      I am using a cortex M0 microcontroller. i am giving a  sine wave input of 1 Hz. this input signal is given to a 12 bit adc and this converted data is given to uart This uart data Im plotting through a serial plotter.



      how to get a smooth sine wave? from the uart data.

        • Re: reproducing sine wave accurately

          So many things could be wrong that it's hard to know where to start suggesting.


          What does you code do, and where did it come from.


          How fast are you sampling, how is the data being presented to the UART (binary, text ???)


          What hardware and programming/debugging tools are you using.


          The amplitude of the samples may be a clue: The 12 bit ADC will give readings from 0 - 4097, but your plot is showing data in the range 7.5E8 to 9.5E8 - that's not right.



          5 of 5 people found this helpful
            • Re: reproducing sine wave accurately
              Jan Cumps

              I found this function in the attachment:


              int32_t  adc_sine(void)
              int32_t u32Result;
              unsigned char buffer[20];
                  // Set reference voltage to AVDD
              //    // Power on ADC
                 // while (u8ADF == 0);
                  u32Result = ADC_GET_CONVERSION_DATA(ADC, 1);
              return u32Result;


              The code does not show the main loop. But I think it's calling adc_sine continuously


              It's initialising , setting the reference of the ADC each time and kicking it off in continuous mode.

              It says it samples continuously, but a single sample is taken then formatted into a string and  sent out to the serial bus (commented out test code says at speed 9600).

              It's going to be too slow. I think it's too slow without the UART write too.


              Everything before this line should not be in the loop:

               u32Result = ADC_GET_CONVERSION_DATA(ADC, 1);



              The writing to the UART should be made efficienter too. Maybe gather a series of samples in a uint32_t buffer (controlled by a timer tick?), then writing when done sampling (I don't know if the display has time as horizontal axis, or measurement count ...)

              4 of 4 people found this helpful
                • Re: reproducing sine wave accurately

                  He's using sprintf with %d, so not sending the number, but an ASCII representation. I don't know what his serial plotter is doing, but maybe it is mashing up the characters into nonsense words or long words. Also could be an endian issue or anything. Hardly any info : (

                  • Re: reproducing sine wave accurately

                    Just out of curiosity I tried seeing what ASCII digits and dots and the comma he's got in that string (it is "%d, ") would look like decoded as an integer.

                    Any 4 of those range of ASCII characters, seems to give values of the order of 10^8. e.g. "12, " gives 8e8. I wonder if his serial plotter is trying to interpret the stream as a load of 32-bit ints, or something like that..

                • Re: reproducing sine wave accurately
                  Jan Cumps

                  gnsh , to see if the samples make sense, can you test the program with a DC input of 0 V, 1 V, 2 V and 3 V?

                  That should give stable lines, whatever the speed of your program would be.

                  We can then see (if line 1 is 0, if line 3 = 2 x line 2 level and line 4 = 3 x line 2 level) that the samples are correctly sent to the plotter.

                  Then, if that's the case, we can work on speeding it up.

                  2 of 2 people found this helpful
                  • Re: reproducing sine wave accurately



                    Could you open a serial monitor program (i.e. PuTTY) and show us what it is sending? I'm willing to bet that the issue is on the side of SerialPlot. I had never heard of SerialPlot, but it is super useful! Now I don't have to use Processing to create simple charts.


                    I was able to recreate some semblance of your setup with a 1hz sine wave. I'm using an arduino MKR 1010 with its onboard ADC. I think the trick was in how i sent data out the port, then how I asked SerialPlot to display it. I was able to get a cleanish sine wave recreated; although it didn't work until I had gone into the "Data Format" tab and chose ASCII.


                    Any incorrect setting would result in a garbage plot that didn't reflect the actual data.


                    This is what my data look like on the serial port:


                    Sampling at 10Hz:

                    4 of 4 people found this helpful