I have my MAX335 connected to the MISO, MOSI, SS and SCK.
MISO linked to MAX335 DIN
MOSI linked to MAX335 DOUT
SCK linked to MAX335 SCLK
and SS linked to MAX335 CS
From page 8 of the Datasheet, I see the programming table.
so basically if I want to set Switch 7 to on I must write 1 to D7 etc.
So 10001000 would be switch 7 and 3 in the closed position.
How would I write that to the Shift Register.
Then how do I clear it ? just writing "00000000" should do it ?
(I have LED's on Sw 0 to 7)
My very BASIC code below I am trying to switch on SW7 and SW3 of the MAX335
//Include SPI library
#include <SPI.h>
#include <Wire.h>
//When Using the SPI library, you only have to worry
//about picking your slave select
//By Default, 11 = MOSI, 12 = MISO, 13 = CLK
//int SS = 10; //SPI Slave Select
//testByte is D7 on and D3 on (1)
byte testByte = 0b10001000;
void setup()
{
Serial.begin(38400);
Serial.println("Start Program\n");
//Set Pin Direction
//Again, the other SPI pins are configured automatically
pinMode(SS, OUTPUT);
//Initialize SPI
SPI.begin();
}
void setN1()
{
Serial.println("Sending 1 on D7 and 1 on D3...");
digitalWrite(SS, LOW);
SPI.transfer(1);
SPI.transfer(0);
SPI.transfer(0);
SPI.transfer(0);
SPI.transfer(1);
SPI.transfer(0);
SPI.transfer(0);
SPI.transfer(0);
delay(500);
digitalWrite(SS, HIGH);
//ALSO TRIED THIS:
//Serial.println("Sending 1 on D7 and 1 on D3...");
//digitalWrite(SS, LOW);
//SPI.transfer(testByte);
//delay(500);
//digitalWrite(SS, HIGH);
}
void loop()
{
//this should turn on LED 7 and 3
setN1();
delay(10000);
}
Hi, I went through the 74HC595 tutorial. It is not using the SPI library. and that is what I want to try to achieve. or am I missing something? From various posts, I see some recommend the direct approach and others say SPI.h is easier to use. either way, I am stuck at the moment, just trying to get my basic piece of code to turn on led 7 and 3, using the built-in SPI functions.
Still no output.
Did confirm the MOSI is on the MAX335 DIN, MISO on the DOUT (Although the Datasheet DOUT is not needed if I am not daisy chaining max335's )
Checked My SS goes LOW and HIGH as per program.
(Even connected LED on SS to see when HIGH and LOW ) But removed in case the pullup resistor caused the issue
I appreciate that you want to do it differently, but is the tutorial so bad that you can't get the chip to work with the ideas presented there? It might let you eliminate any lingering hardware or wiring issues.
From the Datasheet of the Max335 IF V- is connected to ground the chip operates a 10-30V
Since I like it in the 4.5 to 20v range, V- is NOT connected to ground.
I don't have a pullup resistor to CS as I set CS ti HIGH on the ATMEGA, and measuring the Voltage is stays on 5V except when I do the writing to the register as per the Datasheet.
VL (Chip Reset needs to be above 0.8v) if it falls below 0.8v the chip resets, and registers a cleared. I keep it also on HIGH via the ATMEGA on a seperate pin. (would you suggest pullup resistors on the CS and VL Lines rather than controlling it with the ATMEG DOUT ?)
I also confirmed my MOSI line (atmega) is connected to the DIN (MAX335)
The tutorial is not that bad at all.
the main thing is it is not using the built in SPI library.
(I did try it without using the SPI library, with same not working result.)
I have double and tripple checked the wiring.
The reason for the MAX335 vs the 74HC595 is the MAX has a com and a normally open pin. hence you can use it as a "relay" completing a circuit isolated from the control hardware. similar to using optocouplers With the 74HC595 to do the same, I have to add transistors, to still control a small 5v relay od SS switch as I do not want any current or voltage from the circuit to traverse the latch. significantly making the pcb larger and more component heavy.
I am just trying to figure out why my program does not work as expected.
I have updated my code, and will post it below (added some debug output on the RS232 line)
//Include SPI library
#include <SPI.h>
#include <Wire.h>
//When Using the SPI library, you only have to worry
//about picking your slave select
//By Default, 11 = MOSI, 12 = MISO, 13 = CLK
//int SS = 10; //SPI Slave Select
//set digits according to the MAX335 Table1, simulating a 3x4 keypad.
byte clearBuf = 0b00000000;
byte digit1 = 0b00001100; //example: witch on normally open 2 and 3
byte digit2 = 0b00000110;
byte digit3 = 0b00100100;
byte digit4 = 0b10001000;
byte digit5 = 0b10000010;
byte digit6 = 0b10100000;
byte digit7 = 0b01001000;
byte digit8 = 0b01000010;
byte digit9 = 0b01100000;
byte digit0 = 0b00010010;
byte enter = 0b00110000;
byte backsp = 0b00011000;
int LED = 4;
int VL = 8;
char x="";
void setup()
{
Serial.begin(38400);
Serial.println("Start Program\n");
//Set Pin Direction
//Again, the other SPI pins are configured automatically
pinMode(SS, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(VL, OUTPUT);
digitalWrite(VL, HIGH);
SPISettings max335(16000000, MSBFIRST, SPI_MODE0);
//Initialize SPI
SPI.begin();
SPI.beginTransaction(max335);
}
void loop()
{
digitalWrite(LED, HIGH);
setMAX335(digit1);
delay(1000);
setMAX335(digit2);
delay(1000);
setMAX335(digit3);
delay(1000);
setMAX335(digit4);
delay(1000);
setMAX335(digit5);
delay(1000);
setMAX335(digit6);
delay(1000);
setMAX335(digit7);
delay(1000);
setMAX335(digit8);
delay(1000);
setMAX335(digit9);
delay(1000);
setMAX335(digit0);
delay(1000);
setMAX335(backsp);
delay(1000);
setMAX335(clearBuf);
delay(1000);
digitalWrite(LED, LOW);
Serial.println("---------------------------------");
delay(5000);
}
void setMAX335(byte Digit)
{
Serial.print("Sending: ");
digitalWrite(SS, LOW);
SPI.transfer(Digit);
printBinary(Digit);
digitalWrite(SS, HIGH);
Serial.println(" - Send Complete");
}
void printBinary(byte inByte)
{
for (int b = 7; b >= 0; b--)
{
Serial.print(bitRead(inByte, b));
}
}
At the risk of what I am risking, I will tell you that your code compiled and runs in the wokwi simulator at wokwi.com.
One of the parts you can add to a simulation is an 8 bit logic analyzer. There's some fiddling with actually seeing the output of a test run, but it would work as a means of showing you exactly what the pins of interest are doing… allowing you to compare it to what should be happening.
Cheaper than buying real instruments, and so far as I have see, the wokwi simulation would be spot-on for taking a peek at what is going on.
The fact that neither way (SPI or hand-made signaling) has worked leaves me wondering about the wiring.
Are you using your PCB board (corrected!)? How much trouble would it be to bread box a test of that chip and an Arduino, just and only?
I am beginning to think instead of using max335 (SPI) I should maybe try ADG715 from analog devices. (same 8 pole SPST switch but use i2c rather than SPI and there is an Ardunio library for it as well)
A breadboard is a bit difficult since the package is a surface-mount chip. I would need to make a pcb with some pins for the BB. Currently, I am giving up on SPI and seriously considering i2c although the chip is 3x the price.
My code runs fine on the MEGA328p as well, with debug output showing correctly. but no response from the max335. using test equipment, I can see the low and high lines work as they should when they should. it is just the SPI data I can't capture to see if the bits are going with the rising edge of the time signal.
since the ADG is also the same package size maybe I should just make 2 PCBs for breadboard experimentation. one for the max and one for the adg.
did you figure it out? I'm also having trouble with the MAX335.
It appears that something strange is going on with supply pins
I have tried a few configurations for the power supply:
V+ = 5V, VL = 5V, V- = GND No luck for me here
V+ = 15V, VL = 5V, V- = -15V
V+ = 15V, VL = 5V, V- = GND
For the last two, after I write to the register I get no response. However, if I unplug the V+ and V- supply so that those pins are floating then the correct switches close (only after I have written to the register). If I reconnect either V+ or V- while any switches are closed then the switches open again. If I leave V+ and V- floating I cannot update the switches. If I start with V+ and V- floating then nothing happens when I write to the register.
The datasheet for the MAX335 says that the max SCLK frequency is 2.1MHz. I set the SCLK frequency lower than this value and suddenly everything worked. I found that any frequency faster than 1.9999MHz will not work.
I connected LEDs to the switches so that I could see their operation easily in my testing.
here is my code:
#include <SPI.h>
int CS = 10; // chip select pin
byte LEDs_ON = B00010001;
byte LEDs_OFF = B00000000;
void setup() {
SPI.begin();
SPI.beginTransaction(SPISettings(1900000, MSBFIRST, SPI_MODE0)); // the data sheet for the MAX335 says the SCLK max frequency is 2.1MHz; however, the frequency cannot be faster than 1.9999 MHz for it to work
pinMode(CS, OUTPUT);
digitalWrite(CS,HIGH);
delay(1000);
}
void loop() {
digitalWrite(CS, LOW);
SPI.transfer(LEDs_ON);
digitalWrite(CS, HIGH);
delay(1000);
digitalWrite(CS, LOW);
SPI.transfer(LEDs_OFF);
digitalWrite(CS, HIGH);
delay(1000);
}
Perhaps you are not able to use the chip at its specified maximum because of your physical layout, lead dress or failure to provide decoupling capacitor(s) as might be called for.
Some time with the data sheet might shed some light on the matter, or research on good practices when dealing with relative high frequency circuits.
I will say I am amazed at what works what shouldn't all the time.
Which you might want to think about, as just now you may be at the hairy edge of reliability for the same reasons.
At least keep this in mind. If things start not working perfectly, tests might involve fixing your hardware up or lowering the clock rate, first solution preferred.
thanks for the input alto777. I have this on a breadboard, and I am using 0.1u and 10u decoupling caps on the supply pins. I also tried with and without 100pF on MOSI, but I did not find a difference with or without the 100pF.
I've used many SPI devices with Arduino in the past and never run into this issue, so it took me awhile to even test a lower clock frequency.
I definitely wont move forward using 1.9999MHz as my clock frequency as I would worry about reliability. I may even go down to 1MHz as I don't really care about the speed. I was just curious where the cutoff was for my current setup and found it to be 1.9999MHz.
I'm all ears for specific suggestions on how to get this working at 2.1MHz. Not that I care for this project but would be good to learn and might be useful someday in the future.