Hackster is hosting Hackster Cafe: Open Hardware Summit. Watch the stream live on Friday!Hackster is hosting Hackster Cafe: Open Hardware Summit. Stream on Friday!
Lithium ION
Published © GPL3+

PWM as DAC - Secret of Arduino

Let me show how the signal processing works, The basic concept of PWM and Low pass filter can generate a signal comparable to DAC.

IntermediateFull instructions provided2 hours282
PWM as DAC - Secret of Arduino

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1

Software apps and online services

PCBWAY

Story

Read more

Custom parts and enclosures

Excel File Sine calculations

Schematics

Example 1 Sinewave

Example 2 Dc signal and Potentiometer

Code

Example 1 Sinewave

Arduino
Sinewave direct
const int analogOutPin = 9;  
int outputPWM = 0; 
int data;
unsigned int inp[50]={576,
639,
700,
758,
812,
862,
906,
943,
974,
998,
1014,
1022,
1022,
1014,
998,
975,
944,
906,
862,
813,
759,
701,
640,
577,
513,
449,
386,
325,
267,
212,
163,
119,
81,
50,
26,
10,
2,
2,
10,
26,
49,
80,
117,
161,
210,
265,
322,
383,
446,
510,
};

void setup() {
  Serial.begin(9600);
}

void loop() {
  int i;
  for(i=0; i<50; i++){
  data = inp[i];
  outputPWM = map(data, 0, 1024, 0, 255);
  delay(10);
  analogWrite(analogOutPin, outputPWM);
  }
}

Example 2 Dc signal and Potentiometer

Arduino
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9;  // Digital pin 9
int potValue = 0;  // value read from the pot
int outputPWM = 0;  // value output to the PWM (analog out)
int Duty = 0;
int delaytime = 1000; 

void setup() {
  Serial.begin(9600);
}

void loop() {
  // read the analog in value:
  potValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  outputPWM = map(potValue, 0, 1023, 0, 255);
  Duty = map(outputPWM, 0, 255, 0, 100 );
  // change the analog out value:
  analogWrite(analogOutPin, outputPWM);

  // print the results to the Serial Monitor:
  Serial.print("Pot Value = ");
  Serial.print(potValue);
  Serial.print("\t Output PWM = ");
  Serial.print(outputPWM);
  Serial.print("\t Duty = ");
  Serial.println(Duty);

  delay(delaytime); // change the frequency
}

Python Script to generate sine Array

Python
import math

# Fill the number of samples
num_points = 1000

# Calculate the step size
step_size = 2 * math.pi / num_points

# Initialize the lookup table
sine_lookup_table = []

# Generate the sine wave values and fill the lookup table
for i in range(num_points):
    # Calculate the sine value
    sine_value = math.sin(i * step_size)
    
    # Scale the sine value to fit within the 12-bit range (0 to 4095)
    # for 10bit replace with 511.5
    # for 12 bit replace with 2147.5
    # for 15 bit 16383.5
    scaled_value = int((sine_value + 1) * 16383.5)
    
    # Append the scaled value to the lookup table
    sine_lookup_table.append(scaled_value)

# Print the sine wave lookup table
print(sine_lookup_table)

Credits

Lithium ION
59 projects • 37 followers
A passionate electronics DIY boy. Currently improving in Embedded systems, soldering and programming.
Contact

Comments

Please log in or sign up to comment.