Thursday, February 26, 2015

AD9850 Arduino sine wave generator 0-40MHz

AD9850 Arduino sine wave generator 0-40MHz

Recently I got one of these cheap (6$) DDS modules from China, and started playing with.
You only need to send a decimal number to the unit, and this board generates a nice sine wave with the frequency of your number.

My first try was with a design and sketch I found on the internet.
Later I added functionality of entering the frequency by a number keyboard


Connect the board as described in the sketch

This is how the DDS board looks



ZOUT1 and ZOUT2 are the outputs for sine wave, the pins are labeled on the bottom of the board.





here is the code for Arduino.
I only copied this code, reference is in the code comments.

/*
 * A simple single freq AD9850 Arduino test script
 * Original AD9851 DDS sketch by Andrew Smallbone at www.rocketnumbernine.com
 * Modified for testing the inexpensive AD9850 ebay DDS modules
 * Pictures and pinouts at nr8o.dhlpilotcentral.com
 * 9850 datasheet at http://www.analog.com/static/imported-files/data_sheets/AD9850.pdf
 * Use freely
 */

 #define W_CLK 8       // Pin 8 - connect to AD9850 module word load clock pin (CLK)
 #define FQ_UD 9       // Pin 9 - connect to freq update pin (FQ)
 #define DATA 10       // Pin 10 - connect to serial data load pin (DATA)
 #define RESET 11      // Pin 11 - connect to reset pin (RST).

 #define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }

 // transfers a byte, a bit at a time, LSB first to the 9850 via serial DATA line
void tfr_byte(byte data)
{
  for (int i=0; i<8; i++, data>>=1) {
    digitalWrite(DATA, data & 0x01);
    pulseHigh(W_CLK);   //after each bit sent, CLK is pulsed high
  }
}

 // frequency calc from datasheet page 8 = <sys clock> * <frequency tuning word>/2^32
void sendFrequency(double frequency) {
  int32_t freq = frequency * 4294967295/125000000;  // note 125 MHz clock on 9850
  for (int b=0; b<4; b++, freq>>=8) {
    tfr_byte(freq & 0xFF);
  }
  tfr_byte(0x000);   // Final control byte, all 0 for 9850 chip
  pulseHigh(FQ_UD);  // Done!  Should see output
}

void setup() {
 // configure arduino data pins for output
  pinMode(FQ_UD, OUTPUT);
  pinMode(W_CLK, OUTPUT);
  pinMode(DATA, OUTPUT);
  pinMode(RESET, OUTPUT);

  pulseHigh(RESET);
  pulseHigh(W_CLK);
  pulseHigh(FQ_UD);  // this pulse enables serial mode - Datasheet page 12 figure 10
}

void loop() {


  sendFrequency(2e6);  // freq 2MHz, type any value you wish in Hz, e.g. 123 will give 123Hz

  delay (10); // put delay or not, it doesn't matter 
  

}

Friday, February 6, 2015

LM75 Temperature Sensor On Arduino


Wire the LM75 in the above way. SDA goes on A4 and SCL on A5. 

Get the I2C library for arduino

Here is an example code which reads the temperature to the full degree C. 
//for lm75
#include <Wire.h>
#define SensorAdresse 0x48 // I2C address of the LM75
#define TEMP 0  // temperature register
char dataString[7]; // temp as string: (-xx)x.x
double temp; // temp as double
int tgtemp; //val to store temperature


//---------- setup devices----------------
void setup()
{Serial.begin(9600); //turn on serial port
   Wire.begin(); //turn on I2C
}

 //----------temperature measure section-----------------


double get_LM75_temperature(int device, int regx) 
{
  int8_t msb;
  int8_t lsb;
  int8_t msb1;
  Wire.beginTransmission(SensorAdresse + device);
  Wire.write(regx);
  Wire.endTransmission();
  Wire.beginTransmission(SensorAdresse + device);
  Wire.requestFrom(SensorAdresse + device, 2);
  if (Wire.available()) {
     msb1 = Wire.read();
     msb = msb1 << 1; 
     lsb = Wire.read();
  }
  
  lsb = (lsb & 0x80 ) >> 7; 
  Wire.endTransmission();
   tgtemp = (msb /2);
   
  
  

}//-------------end temp measure section------------


 void loop()
{
  temp = get_LM75_temperature(0, TEMP);
   
Serial.println(tgtemp);
delay (500);

}