GENERATE VARYING VOLTAGE

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
cdb0ewm
 
Posts: 48
Joined: Tue Nov 04, 2014 6:30 pm

GENERATE VARYING VOLTAGE

Post by cdb0ewm »

I'm trying to generate DC voltage that I can vary from 0 to 5v. There are decent methods using an RC circuit but they seem to have drawbacks. I saw your MCP4725 board and was wondering if this would work. If so, can I use it to generate 2 sets of voltages to simulate a joystick - x voltages and y voltages.

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: GENERATE VARYING VOLTAGE

Post by adafruit_support_bill »

This board will generate a 0-5v output. You can connect as many as 2 of them to your microcontroller, so you have do X/Y output to simulate a joystick.

https://learn.adafruit.com/mcp4725-12-b ... l/overview

User avatar
cdb0ewm
 
Posts: 48
Joined: Tue Nov 04, 2014 6:30 pm

Re: GENERATE VARYING VOLTAGE

Post by cdb0ewm »

Thank you -- just bought 2 boards. Now I'll have to figure out how to hook the up

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: GENERATE VARYING VOLTAGE

Post by adafruit_support_bill »


User avatar
cdb0ewm
 
Posts: 48
Joined: Tue Nov 04, 2014 6:30 pm

Re: GENERATE VARYING VOLTAGE

Post by cdb0ewm »

It looks like they get hooked up in 'parallel' and I vary their addresses in the sketch

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: GENERATE VARYING VOLTAGE

Post by adafruit_support_bill »

You also have to tie the A0 address pin of one of the boards to VDD.
A0 allow you to change the I2C address. By default (nothing attached to A0) the address is hex 0x62. If A0 is connected to VDD the address is 0x63. This lets you have two DAC boards connected to the same SDA/SCL I2C bus pins.

User avatar
cdb0ewm
 
Posts: 48
Joined: Tue Nov 04, 2014 6:30 pm

Re: GENERATE VARYING VOLTAGE

Post by cdb0ewm »

Thanks -just got 2 D to A breakout boards and got them wired/soldered up.

For my joystick sketch the X and y inputs from my iphone app have a range of -1032 to 1032 in each axis.

I would like to use the map function to translate this range to hex values that can be outputted by the boards.

I just not sure how to use the map function with hex

Any help would be suggested.

Thanks

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: GENERATE VARYING VOLTAGE

Post by adafruit_support_bill »

Integers are integers. You can convert between decimal, hex or even binary easily. The processor and DAC chip don't care which you use since it is all converted to binary internally.

The calculator application on your computer probably has a "programmer" mode that will do decimal to hex conversions.

User avatar
cdb0ewm
 
Posts: 48
Joined: Tue Nov 04, 2014 6:30 pm

Re: GENERATE VARYING VOLTAGE

Post by cdb0ewm »

So far so good.

I got one board working but being a novice, I'm not sure what the following means or how to implement in my sketch.

"We break out the ADDR pin so you can connect two of these DACs on one I2C bus, just tie the ADDR pin of one high to keep it from conflicting"

See part of my sketch

void joystickJoyMoveRightCallback(int value) {
/* For x direction */
dac.begin(0x62);
pot = map(value, -1032, 1032, 0, 4113);
dac.setVoltage(pot, false);
}
void joystickJoyMoveBackwardCallback(int value) {
/* For y direction */
dac.begin(0x63);
pot = map(value, -1032, 1032, 0, 4113);
dac.setVoltage(pot, false);
}

Thanks

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: GENERATE VARYING VOLTAGE

Post by adafruit_support_bill »

Post your complete code. It looks like you are trying to access two dacs with only one dac object.

User avatar
cdb0ewm
 
Posts: 48
Joined: Tue Nov 04, 2014 6:30 pm

Re: GENERATE VARYING VOLTAGE

Post by cdb0ewm »

Here you go...thanks

It is based on the Arduino Manager app

Code: Select all

#include <SPI.h>
#include "Adafruit_BLE_UART.h"
#include "IOSControllerForBluefruit.h"
#include <Wire.h>
#include <Adafruit_MCP4725.h>

Adafruit_MCP4725 dac;
int pot;

void doWork();
void doSync(char *variable);
void processIncomingMessages(char *variable, char *value);
void processOutgoingMessages();
void deviceConnected();
void deviceDisconnected();

IOSControllerForBluefruit iosController(&doWork,&doSync,&processIncomingMessages,&processOutgoingMessages,NULL,NULL); 

void setup()
{
  Serial.begin(9600);
  Serial.println("Hello!");

  pinMode(10,OUTPUT);
 digitalWrite(10,LOW);

}

void loop() {
  
  iosController.loop();
}

void doWork() {
}

void doSync(char *variable) {
}

void processIncomingMessages(char *variable, char *value) {
  if (strcmp(variable,"JoyX") == 0) {

         joystickJoyMoveRightCallback(atoi(value));
  
    }

  if (strcmp(variable,"JoyY") == 0) {

      joystickJoyMoveBackwardCallback(atoi(value));
    }
}

void processOutgoingMessages() {
}

void joystickJoyMoveRightCallback(int value) {
/* For x direction */

     dac.begin(0x62);
     pot = map(value, -1032, 1032, 0, 4113);	
     dac.setVoltage(pot, false);
   }
void joystickJoyMoveBackwardCallback(int value) {
/* For y direction */

     dac.begin(0x63);
     pot = map(value, -1032, 1032, 0, 4113);	
     dac.setVoltage(pot, false);
   }
Last edited by adafruit_support_bill on Tue May 12, 2015 8:52 pm, edited 1 time in total.
Reason: please use the </> button when submitting code. press </>, then paste your code between the [code] [/code] tags.

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: GENERATE VARYING VOLTAGE

Post by adafruit_support_bill »

If you want to control two devices you need to create two instances of the device object:

Code: Select all

Adafruit_MCP4725 dacX
Adafruit_MCP4725 dacY;
Then call begin() on each just once in your setup() function.

User avatar
cdb0ewm
 
Posts: 48
Joined: Tue Nov 04, 2014 6:30 pm

Re: GENERATE VARYING VOLTAGE

Post by cdb0ewm »

Thank you for continuing to help me. I'm not sure of two things.

I am unclear as to the format for the begin() statements and do the
dac.begin(0x63); statements need to be changed to dacX and dacY

Thanks again

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: GENERATE VARYING VOLTAGE

Post by adafruit_support_bill »

You need:

Code: Select all

dacX.begin(0x62);
dacY.begin(0x63);
And when you write to them, you need to address them as dacX and dacY also.

User avatar
cdb0ewm
 
Posts: 48
Joined: Tue Nov 04, 2014 6:30 pm

Re: GENERATE VARYING VOLTAGE

Post by cdb0ewm »

First off..Thanks again for your help.
I made the changes you suggested but it seems that only one of the D to A chips is being recognized. Just to make sure I've got the breadboard setup right, they are both wired as shown in the LEARN example on your site. As you told me I connected the A0 pin on the second D to A board to 5v.

Here's my sketch. I would appreciate your feedback

Code: Select all

#include <SPI.h>
#include "Adafruit_BLE_UART.h"
#include "IOSControllerForBluefruit.h"
#include <Wire.h>
#include <Adafruit_MCP4725.h>


Adafruit_MCP4725 dacX;
Adafruit_MCP4725 dacY;
int pot;

void doWork();
void doSync(char *variable);
void processIncomingMessages(char *variable, char *value);
void processOutgoingMessages();
void deviceConnected();
void deviceDisconnected();

IOSControllerForBluefruit iosController(&doWork,&doSync,&processIncomingMessages,&processOutgoingMessages,NULL,NULL); 

void setup()
{
   dacX.begin(0x62);
  dacY.begin(0x63);
  Serial.begin(9600);
  Serial.println("Hello!");

  pinMode(10,OUTPUT);
 digitalWrite(10,LOW);
 
}

void loop() {
  
  iosController.loop();
}

void doWork() {
}

void doSync(char *variable) {
}

void processIncomingMessages(char *variable, char *value) {
  if (strcmp(variable,"JoyX") == 0) {
Serial.println(variable);
         joystickJoyMoveRightCallback(atoi(value));
  
    }

  if (strcmp(variable,"JoyY") == 0) {
Serial.println(variable);
      joystickJoyMoveBackwardCallback(atoi(value));
    }
}

void processOutgoingMessages() {
}

void joystickJoyMoveRightCallback(int value) {
/* For x direction */

     pot = map(value, -1032, 1032, 0, 4113);   
     dacX.setVoltage(pot, false);
   }
void joystickJoyMoveBackwardCallback(int value) {
/* For y direction */

     pot = map(value, -1032, 1032, 0, 4113);   
     dacY.setVoltage(pot, false);
  }
Last edited by adafruit_support_bill on Fri May 15, 2015 5:42 am, edited 1 time in total.
Reason: Please use the '</>' button when posting code. Paste your code between the [code] tags.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”