pseudo random number generator and PWM output

hi there,
i m doing a project that need to provide signal to an polarization scrambler.
1st. i need to randomly generate 0 or 1 (square wave).
2nd. i need to do pwm nad make the random signal sinusoidal.
anyone could help me with the code or hardware connection?

thanks a lot

provide signal to an polarization scrambler.

What is this? Link please.

1st. i need to randomly generate 0 or 1 (square wave).

Do you mean the times of the signal need to be random or that the appearance of this wave has to be random?

2nd. i need to do pwm nad make the random signal sinusoidal.

I understand all those words but that makes no sense. What do you want, please explain properly.

Here a starter with all elements you mentioned, but it might be useless as I don't know what a polarization scrambler is

uint8_t pwm_value = 0;

void setup()
{
  Serial.begin(115200);
  Serial.println("polarization scrambler 0.1");

  randomSeed(analogRead(A0)); // to have a bit of a random start
}

void loop()
{
  int x = random(2);
  pwm_value <<= 1 + x;
  analogWrite(9, pwm_value);

  // optional
  Serial.print(millis());
  Serial.println("\t");
  Serial.println(pwm_value);
}

@robtillaart where is the

i need to do pwm nad make the random signal sinusoidal.

bit?

oops, redesign :wink:

uint8_t pwm_value = 0;

float signal = 0;

void setup()
{
  Serial.begin(115200);
  Serial.println("polarization scrambler 0.1");

  randomSeed(analogRead(A0)); // to have a bit of a random start
  pinMode(4, OUTPUT);
}

void loop()
{
  int x = random(2)
  digitalWrite(4, x);  // your square wave

  signal = signal + 0.0001;
  if (signal > 2*PI) signal = 0;
  int pwm_value = 127 + 127 * sin(signal);  // make a sinusoidal pwm value

  if (x == 0) pwm_value = 127; // the random square wave works as a gating for the sinus signal making is 0 at random times and sinusoidal the other times.

  analogWrite(9, pwm_value);

  // optional
  Serial.print(millis());
  Serial.print("\t");
  Serial.print(x);
  Serial.print("\t");
  Serial.println(pwm_value);
}

I think I did the other way around, I made the sinus signal random. If this is not what you want can you post a drawing of how the PWM signal should look like (OK randomized will always be different) or as grumpy says, we need proper requirements.

Grumpy_Mike:

provide signal to an polarization scrambler.

What is this? Link please.

1st. i need to randomly generate 0 or 1 (square wave).

Do you mean the times of the signal need to be random or that the appearance of this wave has to be random?

2nd. i need to do pwm nad make the random signal sinusoidal.

I understand all those words but that makes no sense. What do you want, please explain properly.

well, thanks a lot for the help.
just ignore the polarization scrambler. it is an optical device..
what i want just are:
1st, use Arduino to generate random signal or random pulse, just 0 or 1, such as 001010010101001001 which the output to an oscilloscope will be square wave.
2nd, do pulse width modulation with the signal to make it sinusoidal.
i might need code and circuit design for this. the output is random sinusoidal signal.

Grumpy_Mike:
@robtillaart where is the

i need to do pwm nad make the random signal sinusoidal.

bit?

pseudo random bit sequence i think.

robtillaart:
oops, redesign :wink:

uint8_t pwm_value = 0;

float signal = 0;

void setup()
{
  Serial.begin(115200);
  Serial.println("polarization scrambler 0.1");

randomSeed(analogRead(A0)); // to have a bit of a random start
  pinMode(4, OUTPUT);
}

void loop()
{
  int x = random(2)
  digitalWrite(4, x);  // your square wave

signal = signal + 0.0001;
  if (signal > 2*PI) signal = 0;
  int pwm_value = 127 + 127 * sin(signal);  // make a sinusoidal pwm value

if (x == 0) pwm_value = 127; // the random square wave works as a gating for the sinus signal making is 0 at random times and sinusoidal the other times.

analogWrite(9, pwm_value);

// optional
  Serial.print(millis());
  Serial.print("\t");
  Serial.print(x);
  Serial.print("\t");
  Serial.println(pwm_value);
}



I think I did the other way around, I made the sinus signal random. If this is not what you want can you post a drawing of how the PWM signal should look like (OK randomized will always be different) or as grumpy says, we need proper requirements.

Pulse-density modulation - Wikipedia more like the graph on this page. yet its random sine wave based on random 1 or 0

what i want just are:
1st, use Arduino to generate random signal or random pulse, just 0 or 1, such as 001010010101001001 which the output to an oscilloscope will be square wave.
2nd, do pulse width modulation with the signal to make it sinusoidal.
i might need code and circuit design for this. the output is random sinusoidal signal.

add 1 - is done with my code posted, read the signal on pin 4.

add 2 - add a low pass filter (resistor + capacitor) after the pin4 output to get "sort of" Analog out

pin 4 ----[ resistor ] ------X-------||------GND

measure with scope on point X.

robtillaart:

what i want just are:
1st, use Arduino to generate random signal or random pulse, just 0 or 1, such as 001010010101001001 which the output to an oscilloscope will be square wave.
2nd, do pulse width modulation with the signal to make it sinusoidal.
i might need code and circuit design for this. the output is random sinusoidal signal.

add 1 - is done with my code posted, read the signal on pin 4.

add 2 - add a low pass filter (resistor + capacitor) after the pin4 output to get "sort of" Analog out

pin 4 ----[ resistor ] ------X-------||------GND

measure with scope on point X.

thanks mate.i ll try the RC, what is the frequency of PWM that i need to do mate to get R and C?